• Home
  • Swinburne University of Technology
  • Foundations of statistics
  • Introduction to R for Statistical Analysis

Introduction to R for Statistical Analysis

Learning R in R Studio PART I #Hello! Do not stress ... follow the instruction ya. #My data is located at my desktop and the pathway is as below: #C:\Usersladambujang\Desktop\R tutorial #So, I will set my working file so R can quickly recognize my working folder setwd("C:/Users/adambujang/Desktop/R") #What are inside the folder "R tutorial" dir() #My aim is to open file name "obesity" in csv format. First of all, I have to make sure that I have file name "obesity" in csv format such as "obesity.csv" before I run below command. mydata <- read.csv("C:/Users/adambujang/Desktop/R/obesity.csv", header=TRU E) #Here, you have renamed the data in R with "mydata" where previously was named as "obesity". You can maintain the name as "obesity" if you want to. #Now, my data is available in R with 100 observations and 20 variables. Open and see the data just by click on "mydata". Can you see the data? #Ask for mean, standard deviation (SD), median, inter-quartile range (IQR), minimum and maximum for numerical variable such as age #Before that, you need to instruct the R to use "mydata". attach (mydata) #Then, conduct the descriptive analysis such as below: > mean (age) [1] 40.57 > sd(age) [1] 5.358379 > median(age) [1] 40 > IQR (age) [1] 7 > min(age) [1] 27 > max (age) [1] 55 #Compare descriptive statistics (i.e. mean, sd, median, IQR) between groups #Compare mean age between gender aggregate(age, list(Gender = gender), mean) 1 Gender x 1 1 40.54167 2 2 40.59615 #Compare standard deviation (SD) age between gender aggregate(age, list(Gender = gender) , sd) Gender x 1 5.449803 1 2 2 5.325704 #Hence, this can be summarized to Gender Mean SD Male 40.5 5.4 Female 40.6 5.3 #Wonderful, now let's do frequency table ... say for gender > table (gender) gender 1 2 48 52 > prop. table (table(gender)) gender 1 2 0.48 0.52 #Based on variable definition, 1=Male and 2=Female. Therefore, here we have 48 (48.0%) male's respondent and 52 (52.0%) female's respondents. #Compare frequency and proportion between groups #Tabulate crosstabulation table between gender and obesity mytable <- table (gender, obesity) > mytable obesity gender 1 2 1 17 31 2 20 32 #Produce row percentage > prop. table(mytable, 1) obesity gender 1 2 1 0.3541667 0.6458333 2 0.3846154 0.6153846 #Produce column percentage > prop. table (mytable, 2) obesity 1 2 gender 1 0.4594595 0.4920635 2 0.5405405 0.5079365 2 #In other words, output can be presented as below table: Status of obesity Yes No Gender n Male 17 (0.46) 31 (0.49) Female 20 (0.54) 32 (0.51) (%) n (%) #Inferential analysis #Estimation confidence interval (CI) of mean #To estimate exercise time > mean (ex_time) [1] 16.16 > t. test (ex_time) $conf. int [1] 15.28403 17.03597 attr(, "conf. level") [1] 0.95 #So, mean duration of exercise time in minute per week is 16.2 minutes. Based on the estimation, the 95%CI of mean exercise time in minute per week is from 15.3 minutes and 17.0 minutes. Meaning, the 95% CI of the mean duration of