0% found this document useful (0 votes)
1K views19 pages

R Lab Manual

Uploaded by

Pushpa Prashanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
1K views19 pages

R Lab Manual

Uploaded by

Pushpa Prashanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 19
Maharaja Education Trust © MIT First Grade College MEI: Department of Computer Applications g REVOLUTION Industrial Suburb, Manandavadi Road, Mysuru -570008, www.mitfgc.in Affiliated to University of Mysore, Accredited by NAAC with *A” Grade, ISO 9001-2015 Certified Institution VISION OF THE INSTITUTE Empower the individuals and society at large through educational excellence; se for a life dedicated to the service of fellow human beings and mother land. MISSION OF THE INSTITUTE To impact holistic education that enables the students to become socially responsive and useful, with roots firm on traditional and cultural values; and to hone their skills to accept challenges and respond to opportunities in a global scenario. Lecture Notes on: R PROGRAMMING LAB Course Code: BCADSC14-LAB Contact Hours: 04 hrs per week Formative Assessment Marks: 25 Summative Exam Marks: 25 Exam Duration: 03hrs Overview The following program problematic comprises of R programming basics and application of several Statistical Techniques using it, The module aims to provide exposure in terms of Statistical Analysis, Hypothesis Testing, Regression and Correlation using R. programming language. Learning Objectives The objective of this Laboratory to make students exercise the fundamentals of statistical analysis in environment. They would be able to analysis data for the purpose of exploration using Descriptive and Inferential Statistics. Students will understand Probability and Sampling Distributions and learn the creative application of Linear Regression in multivariate context for predictive purpose. Prepared by: Asst. Prof. Suhas B Raj Department: Computer Applications 10. Course Outcomes: Install, Code and Use R Programming Language in R Studio IDE to perform basic tasks on Vectors, Matrices and Data frames. Explore fundamentals of statistical analysis in R environment. Describe key terminologies, concepts and techniques employed in Statistical Analysis. Define Calculate, Implement Probability and Probability Distributions to solve a wide variety of problems. Conduct and interpret a variety of Hypothesis Tests to aid Decision Making, Understand, Analyze, and Interpret Correlation Probability and Regression to analyze the underlying relationships between different variables List of Experiments Write a R program for different types of data structures in R. Write a R program that include variables, constants, data types Write a R program that include different operators, control structures, default values for arguments, returning complex objects. Write a R program for quick sort implementation, binary search tree. Write aR program for calculating cumulative sums, and products minima maxima and calculus. Write a R program for finding stationary distribution of markanov chains Write a R program that include linear algebra operations on vectors and matrices. Write a R program for any visual representation of an object with creating graphs usinggraphic functions: Plot (), Hist (), Linechart (), Pie (), Boxplot (), Scatterplots (). Write a R program for with any dataset containing data frame objects, indexing and sub setting data frames, and employ manipulating and analyzing data. Write a program to create an any application of Linear Regression in multivariate context forpredictive purpose. R PROGRAMMING LAB 1. WriteaR program for different types of datastructuresin R. Vector # Creating a character vector character_vector <- ¢ ("apple”, "banana", "cherry") character_vector output: [1] “apple” "banana" "cherry" Matrix # Creating a numeric matrix numeric_matrix <- matrix (1:6, nrow = 2, neol = 3) numeric_matrix output: (1) (2) 63) fj 135 2] 24 6 Lists # Creating a list my_list <- list (name = ¢("John","Daniel","Jack"), age = ¢ (30,53,40), hobbies =c ("reading”, "golf","Gaming")) my_list output: Sname:{1] "John", "Daniel" "Jack" Sage:[1] 30 53 40 Shobbies:{1] "reading" "golf". "Gaming" DataFrame # Creating a data frame data_frame <- data.frame (Name = c ("Alice”, "Bennett", "Charlie"),Age = ¢ (25, 30, 22), Gender =c ("Female”, "Male", "Male")) data_frame output: Name Age Gender 1 Alice 25 Female 2 Bennett 30 Male 3 Charlie 22 Male Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 1|17 R PROGRAMMING LAB Factors # Creating a factor gender <- c ("Male","Female","Male","Female","Male") factor_gender <- factor (gender, levels = ¢ (""Male","Female")) factor_gender output: [1] Male Female Male Female Male Levels: Male Female Array #Creating an Array arr <- array (1:24, dim =e (4,3,2)) arr output: . 1 LU02) 63] 1s 9 12] 2 6 10 Bl 371 [4] 4 8 12 vod LUE) £3) Uj 1317 Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 2\17 R PROGRAMMING LAB 2.Write a R program that include variables, constants, data types. # Define variables radius <-5 radius output-[1] 5 name <- "Alice" name output-[1] "Alice" age <-30L age output:{1] 30 is_student <- TRUE is_student output: [1] TRUE # Constants PI <3.14159265359 paste ("Constant Value:",PI) output:[1] "Constant Value 3.14159265359" GREETING < "Hello, World!" paste ("Constant Value:", GREETNG) output:[1] "Constant Value: Hello, World!" # Data pes print(class(radius)) output: [1] "numeric" print(class(name)) output: [1] "character" print(class(age)) output: [1] "integer" print(class(is_student)) output: [1] "logical" Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 3|17 R PROGRAMMING LAB 3.Write a R program that include different operators, control structures, default values for arguments, returning complex objects # Arithmetic operators asl bed sum_result< ab sum_result output:[1] 15 diff result <- a-b diff result output:[1] 7 product_result <- a*b product_result output-{1] 44 division_result <-a/b division_result output:[1] 2.75 modulus_resultb) { print ("a is greater than b") else if (a pivot] return(e(quick_sort(left), middle, quiet 4 veet = © (2,5,3,6,8,441,3,10) print ("Unsorted Vector") print(veet) output: [1] 253.684 1310 sorted_vector <- quick_sort(vect) print("sorted vector") print(sorted_vector) mena eeen Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 6/17 R PROGRAMMING LAB # Define the structure for a Binary Search Tree node bst_node < fimetion(key) { return (list (key = key, left = NULL tight = NULL)) } # Function to insert a key into the BST insert <- function (root, key) { if (is.null(root)) { return(bst_node(key)) } if (key < root$key) { rootSleft < insert(rootSleft, key) } elseif (key > rootSkey) { rootSright < inse! , key) 5 return(root) 4 # Function to perform an in-order trave in_order_traversal <- function(root) { if (lis.mull(root)) in_order_traversal(rootSleft) cat(rootSkey, "") in_order_traversal(rootSright) r_traversal(bst) outputs] 23.589 Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 7/17 R PROGRAMMING LAB 5.Write a R program for calculating cumul: maxima and calculus sums, and products minima # Sample vector of numbers numbers <- ¢ (1, 2, 3, 4, 5) # Calculate cumulative sum cumulative_sum < cumsum(numbers) cat ( mulative Sum:", cumulative_sum, "\n") output: Cumulative Sum: 1 3 6 10 15 # Calculate cumulative product cumulative_product <- cumprod(numbers) cat("Cumulative Product:", cumulative_product, "\n") output: Cumulative Product; 12 6 24 120 # Calculate minimum and maximum min_value <- min(numbers) ‘max_value 25):\n") subset_data<- data_frame{data_frame$Age > 25, ] print(subset_data) output: Name Age Gender Score 2 Bennett30 Male 92 4 David 28 Male 88 5 Emma 35 Female 95 # Calculate Summary Statistics summary_stats <- summary(data_frame$Score) summary_stats output: Min. Ist Qu. Median Mean 3rd Qu. Max. 78.0 85.0 88.0 87.6 92.0 95.0 # Add a new column data_frame$Grade < ifelse(data_frame$Score >= 90, "A’ print(data_frame) ifelse(data_frame$Score output: Name Age Gender Score Grade 1 Alice 25 Female 85 B 2 Bennete 30 Male 92 A 3 Charlie 22 Male 78 € 4 David 28 Male 88 B 5 Emma 35 Female 95 9 A Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 1417 R PROGRAMMING LAB # Grouping and Aggregation gender_avg_score <- aggregate (data_frame$Score, by = list(data_frameSGender), FUN =mean) colnames(gender_avg_score) <- ¢("Gender", "Avg_Score") print(gender_avg_score) output: Gender Avg_Score 1 Female 90 2 Male 86 SX Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 1S|17 R PROGRAMMING LAB 10.Write a program to create an any application of Linear Regression in multivariate context for predictive purpose. #Load the mtcars dataset data(mtcars) # Explore the dataset head(mtcars) output: Fa mpg cyl disp hp drat wt qsec vs am gear carb zda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 160 110 3.90 2.875 17.02 108 93 3.85 2.320 18.61 258 110 3,08 3,215 19.44 360 175 3.15 3.440 17.02 zda RX4 Wag 21.0 Datsun 710 22.8 Hornet 4 Drive 21.4 Hornet Sportabout 18.7 otro core wo ee Ree ee # Fit a multivariate linear regression m +# We'll predict ‘mpg’ (miles per gallon) ‘model < Im(mpg ~ hp + wt, data = model ‘hp’ (horsepower) and 'wt" (weight) call: Am(formula = mpg ~ hp + wt, data = mtcars) Coefficients: | (Intercept) ap we 37.22727 -0.03177 3.87783 Im(formula hy ars) call: Am(formula = mpg ~ hp + wt, data = mtcars) Coefficients: (Intercept) np we 37,22727 --0,03177 «= -3.87783 Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 16|17 R PROGRAMMING LAB # Print the model summary summary(model) Call: Am(formula = mpg ~ hp + wt, data = mtcars) Error t value Pr(>It|) 59879 23.285 < 2e-16 00903 -3.519 0.00148 Residua: Min 10 Median 30 Max -3.941 -1.600 -0.182 1.050 5.854 Coefficients: Estimate Std. (Intercept) 37.22727 1. hp -0.03177 0. we 3.877830. Signif. codes: 0 + 1 0. 63273 -€.129 1.12e-06 002 *#*" 0,02 *#" 0.05 4 OL SAD Residual standard error: 2.593 on 29 degrees of freedom Multiple R-squared: 0.8268, Adjusted R-squared: 0.8148 Festatistic: 69.21 on 2 and 29 DF, p-value: 9.109e-12 #Make predictions using the model new data < data. frame(hp = ¢(150, 200) 3.5, 4.0) predictions <- predict(model, newdata = new_data) cat("Predicted MPG for new data:\n") print(predictions) output: Predicted Suhas B Raj, Asst Prof. Dept of CA, MIT FGC, Mysore Page 17|17

You might also like