Introduction to R
Statistical Consulting Center University of North Carolina at Greensboro
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 1 / 68
Introduction to R Statistical Consulting Center University of North - - PowerPoint PPT Presentation
Introduction to R Statistical Consulting Center University of North Carolina at Greensboro Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 1 / 68 1. R Programming Basics From
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 1 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 2 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 3 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 4 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 5 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 6 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 7 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 8 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 9 / 68
boxplot(Data)
2 4 6 8 10
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 10 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 11 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 12 / 68
1
2
3
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 13 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 14 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 15 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 16 / 68
Data <- read.table(file="https://www.uncg.edu/mat/qms/datafile1.txt", header=F) Data
## V1 ## 1 2 ## 2 5 ## 3 8 ## 4 9 ## 5 9 ## 6 10 ## 7 11 Now we may use functions to process the data. In the read.table function, the first argument is the specification
the delimiter, which in this case is a space, while the second specifies that the first row of the file does not contain variable name. If the first row contains the name of the variable, then add the option header=TRUE (or header=T). Note that all letters after the equal sign must be capitalized. Many other options can be specified in the read.table function (more on this later).
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 17 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 18 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 19 / 68
Pulse <- read.table(file="https://www.uncg.edu/mat/qms/data.txt",header=T) head(Pulse,5) ## Height Weight Age Gender Smokes Alcohol Exercise Ran Pulse1 Pulse2 Year ## 1 173 57 18 2 2 1 2 2 86 88 93 ## 2 179 58 19 2 2 1 2 1 82 150 93 ## 3 167 62 18 2 2 1 1 1 96 176 93 ## 4 195 84 18 1 2 1 1 2 71 73 93 ## 5 173 64 18 2 2 1 3 2 90 88 93
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 20 / 68
40 60 80 100 60 80 100 120 140 Pulse$Weight Pulse$Pulse1
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 21 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 22 / 68
Y N 60 80 100 120 140 Pulse$Smokes.factor Pulse$Pulse1
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 23 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 24 / 68
Y N
Ran(Y/N) for all Years
Ran Count 5 10 15 93 95 96 97 98
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 25 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 26 / 68
summary(Pulse) ## Height Weight Age Gender Smokes ## Min. : 68.0 Min. : 27.00 Min. :18.00 Min. :1.000 Min. :1.0 ## 1st Qu.:165.2 1st Qu.: 56.25 1st Qu.:19.00 1st Qu.:1.000 1st Qu.:2.0 ## Median :172.5 Median : 63.00 Median :20.00 Median :1.000 Median :2.0 ## Mean :171.6 Mean : 66.33 Mean :20.56 Mean :1.464 Mean :1.9 ## 3rd Qu.:180.0 3rd Qu.: 75.00 3rd Qu.:21.00 3rd Qu.:2.000 3rd Qu.:2.0 ## Max. :195.0 Max. :110.00 Max. :45.00 Max. :2.000 Max. :2.0 ## ## Alcohol Exercise Ran Pulse1 ## Min. :1.000 Min. :1.000 Min. :1.000 Min. : 47.00 ## 1st Qu.:1.000 1st Qu.:2.000 1st Qu.:1.000 1st Qu.: 68.00 ## Median :1.000 Median :2.000 Median :2.000 Median : 76.00 ## Mean :1.382 Mean :2.209 Mean :1.582 Mean : 75.69 ## 3rd Qu.:2.000 3rd Qu.:3.000 3rd Qu.:2.000 3rd Qu.: 82.00 ## Max. :2.000 Max. :3.000 Max. :2.000 Max. :145.00 ## NA's :1 ## Pulse2 Year Smokes.factor Ran.factor ## Min. : 56.0 Min. :93.00 Y:11 Y:46 ## 1st Qu.: 72.0 1st Qu.:95.00 N:99 N:64 ## Median : 84.0 Median :96.00 ## Mean : 96.8 Mean :95.63 ## 3rd Qu.:125.0 3rd Qu.:97.00 ## Max. :176.0 Max. :98.00 ## NA's :1 Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 27 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 28 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 29 / 68
40 60 80 100 60 80 100 120 140 Pulse$Weight Pulse$Pulse1 Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 30 / 68
40 60 80 100 60 80 100 120 140 Pulse$Weight Pulse$Pulse1
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 31 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 32 / 68
reg.fit<-lm(Pulse1~Weight, data=Pulse) summary(reg.fit) ## ## Call: ## lm(formula = Pulse1 ~ Weight, data = Pulse) ## ## Residuals: ## Min 1Q Median 3Q Max ## -26.878
0.422 6.733 71.632 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 86.96948 5.63684 15.429 <2e-16 *** ## Weight
0.08282
0.0425 * ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 13.1 on 107 degrees of freedom ## (1 observation deleted due to missingness) ## Multiple R-squared: 0.03789, Adjusted R-squared: 0.0289 ## F-statistic: 4.214 on 1 and 107 DF, p-value: 0.04252
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 33 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 34 / 68
Histogram of residuals(reg.fit)
residuals(reg.fit) Frequency −20 20 40 60 80 10 20 30
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 35 / 68
68 70 72 74 76 78 80 82 −20 20 40 60 fitted(reg.fit) residuals(reg.fit)
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 36 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 37 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 38 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 39 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 40 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 41 / 68
65 70 75 80 85 90
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 42 / 68
60 80 100 120 140
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 43 / 68
1 2 60 80 100 120 140 Smokes Pulse1
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 44 / 68
Y N 60 80 100 120 140 Pulse$Smokes.factor Pulse$Pulse1
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 45 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 46 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 47 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 48 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 49 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 50 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 51 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 52 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 53 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 54 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 55 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 56 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 57 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 58 / 68
Y N 5 10 15
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 59 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 60 / 68
Y N
Ran(Y/N) for all Years
Ran Count 5 10 15 93 95 96 97 98
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 61 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 62 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 63 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 64 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 65 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 66 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 67 / 68
Statistical Consulting Center University of North Carolina at Greensboro Introduction to R 68 / 68