Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sample slides from "Getting Started with R" course

2,186 views

Published on

Sample slides from "Getting Started with R" course, run in collaboration with PRISMTC http://www.prismtc.co.uk/f106-r-starter-course-1/

Published in: Data & Analytics
  • Be the first to comment

  • Be the first to like this

Sample slides from "Getting Started with R" course

  1. 1. © 2012 Heather Turner Part II Using R for Data Analysis 49 / 90
  2. 2. © 2012 Heather Turner Simple Linear Regression A simple linear model is fitted using lm > model1 <- lm(wt ~ gestation, data = infant) > model1 Call: lm(formula = wt ~ gestation, data = infant) Coefficients: (Intercept) gestation -10.0642 0.4643 Specific components of the model fit can be extracted, for example using coef, deviance, fitted or residuals > deviance(model1) [1] 339092.1 50 / 90
  3. 3. © 2012 Heather Turner Model Summary > summary(model1) Call: lm(formula = wt ~ gestation, data = infant) Residuals: Min 1Q Median 3Q Max -49.394 -11.125 0.071 10.106 57.353 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -10.06418 8.32220 -1.209 0.227 gestation 0.46426 0.02974 15.609 <2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 16.66 on 1221 degrees of freedom (13 observations deleted due to missingness) Multiple R-squared: 0.1663, Adjusted R-squared: 0.1657 F-statistic: 243.6 on 1 and 1221 DF, p-value: < 2.2e-16 51 / 90
  4. 4. © 2012 Heather Turner Diagnostics > layout(matrix(1:4, nrow = 2)) > plot(model1) 60 80 100 120 140 −60−202060 Fitted values Residuals q q q q q q q q q q qq q q q q q q q q q q q q q q q qq q q q q q q q qq q q q q q q q q q q q q q q q q qq q q q q qq q q q q q q q q q q q q q q qq q q q q q qq q q q q q q q q q qq qq q q q q q q q q q q q q q q q qq q q q q qq qq q q q q q q qqq q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q qq qq qq qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q qq q q qq q q qq q q q q q q q q q q qq q qq q q q q q q q q qq q q q q qqq q q q q q qq q q q q q q q q q q q qq q q q q q q q qq q q q q q q q q q q q qq qq q q q q qq q qq q q q q q q q q qq q qq q q q q q q q q qq q q q qq q qq q q qqq q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q qqqq q q q qqq q q q q q qqq q q q q q q q q q q q q q q q q qqq q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q qq qq q q q q q q q q q q q q q q q q q q q q q q q qq q q q q qq q q qq q q q q qqq q q qq q q q q q q q q q qq q q q q q q q q q qq q q qq qq qq q q q q q q q q q q q q q q q q q q q qq q q q q q q qq q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qqq q q q q q q qq q q q q qq q q qq q q qq qq q q q q q q q qq q q q q qqq q q q qq q q q q qq qq q q q q q qq q q q qqq q q q q q q q q q q q q q q q q qq q q q q q q q q q q qq q q q q q q q q q qqq q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q qq q q q q q q q q q qqq q q q q qq q qq q q qq q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q qq q q q q q qq q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qqq q q qq q q q q q q q q q q q q q q q q qq q q q q qqq q q q q qq q qq q q q q q qq q qq q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q qq qqq q q q q q qq q q q q q q q q q q q q q q qq q qq q q q q q q qq q qq q q q q q q q qq q q q q q q q q q q q q q q qq q q q q q qq q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q qq q q qq q q q q q q q q q q q q qq q q q q q q q q q qq q Residuals vs Fitted 261 5571100 q q q q q q q q q q qq q q q q q q q q q q qq q q qqq q q q q q q q qqqq q q q q qq q q q q q q q q qq q q q q qq q q q q q q q q q q q q q q qq q q q q q qqq q q q q q q q q qqqq q q q q q q q q q q q q q q q qq q q q q qq qq q q q q qq qqq q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q qq qq qq qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q qq q q q q qq q q qq q q qq q q q q q q q q q q qq q qq q q qq q q q q qq q q q q qqq q q q q q qq q q q q q q q q q qq qq q q q q q q q qq q q q q q q q q q q q qq qq q q qq qq q qq q q q q q q q qqq q qq q q q q q q q q qq qq q qq q q q q q qqq q qq q q q q qq q q q q q q q q q q q q q q q q qqq q q qqqq q qq qqq q q q q q qqq q q q q q q q q q q q q q q q q qqqq q q q q q q qq q q q q q qqq q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q qq qq qq q q q q q q q q q q qq q q q q q q q q q q q q q qq qq q q qq q qq q qqq q q qq q q q q q q q q q qqq q q q q q qq q qq q q qq q q qq q q q q q q q q q q q q q q q q q q q qq q q q q qq qqqq q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qqq qq q q q q qq q q q q qqq q qq q q qq qq q q q q q q q qq q q q q qqq q q q qq q qq q qq qq q q q q q qq q q q qqq q qq q q q q q q q q q q q q q qq q q qq q q q q q q qq q q q q q q q q q qqq q q q q q q q q q q q q q q q qqq q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q qq q q q q q q q q q qq q q q q q qq q qq q q q q q q q q q q q q q q q q qq q q q qq q qq q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q qq q q q q q qq q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q qq q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q qqq q q qq q q q q q q q q q q q q q q q q qq q q q q qqq q q q q qq q qq q q q q q qq q qq qq q q q q q q q q qq q q q q q q q q q q q qq q q q q qq q q qq q q q q q q q q q q q q q q q q q q q q qq q qqqqq qq q q q qq q q q q q q qq q q q q q q q q q qq q q q q q q q q q qq q q q q q q q qq q q q q qq q q q q q q qq qq q q q q q qq q qq q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q qq q q q q q qq qq q q qq q q qq q q qq q q q q q q q q q q q q qq q q q q qq q q q qq q −3 −1 0 1 2 3 −3−113 Theoretical Quantiles Standardizedresiduals Normal Q−Q 261 5571100 60 80 100 120 140 0.00.51.01.5 Fitted values Standardizedresiduals q qq qq q q q q q qq q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q qq q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q qq qq q q q q q q q qq q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q qq qq q q q q q q q q q q q q q q q q q q q q q q q q q qq qq q q q q q q q q qq q q q q q q q q q q q q q q q q q qq q q q q q q q q q q qq qq q q q q q q q qq q q q q q q qq q q q q q q qq q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q qqq qq q q q q qq q q q q qq q q q q q q q q q q qq q q q q q qq q q qq q q q q q q q qq q q q q q q q q q q q q q q q q q q qq q q q q q q qq q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q qq q q q q q q q qq q q q q q q q qq q q q q q q q q q qqq q q q q qq q q q q q q q q q qq q q q q q qq qq q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q qq qq q qq q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q qq q q q q q q q q q q q q q qq q q qq q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q qq q q q q q q q q q q q q q q q q q qq q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q qq qq q q q q qq q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q qq qq q q qq q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q qq q qq q q q q q q q q q q q q q q q q q q q qq qq q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q qqq q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q qq q q qq q q qq q q q q q q q q q q q q q q q q q q q qq q Scale−Location 261 5571100 0.00 0.02 0.04 −2024 Leverage Standardizedresiduals q q q q q q q q q q qq q q q q q q q q q q qq q q qqq q q q q q q q qqqq q q q q qq q q q q q q q q qq q q q q qq q q q q q q q q q q q q q q qq q q qq q qqq q q q q q q q q qqqq q q q q q q q q q q q q q q q qq q q q q qq qq q q q q qq qqq q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q qq qq qq qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q qq q q qq q q q q q q q q q q q q q q qq q qq q q qq q q q q qq q q q q qqq q q q q q qq q q q q q q q q q qq qq q q q q q q q qq q q q q q q q q q q q qq qq q q qq qq q qq q q q q q q q qqq q qq q q q q q q q q qq qq q q q q qq q q qqq q qq q q q q qq q q q q q q q q q q q q q q q q qqq q q qqqq q qq qqq q q q q q qqq q q q q q q q q q q q q q q q q qqqq q q q q q qqq q q q q q qqq q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q qq qq qq q q q q q q q q q q qq q q q q q q q q q qq q q qq qq q q qq q qq q qqq q q qq q q q q q q q q q qqq q q q q q qq q qq q q qq qq qq q q q q q q q q q q q q q q q q q q q qq q q q q qq qqqq q q q q q q qq q q q q q qq q q q q q q q q q q q q q q q q q q q q q q qqq qq q q q q qq q q q q qqq q qq q q qq qq q q q q q q q qq q q q q qq q q q q q q q qq q qqqq q q q q q qq q q q qqq q qq q q q q q q q q q q q q q qq q q qq q q q q q q qq q q q q q q q q q qqq q q q q q q q q q q q q q q q qqq q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q qq q q q q q q q q q qqq q q q q qq q qq q q qq q q q q q q q q q q q qqq q q q qq q qq q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q qq q q q q q qq q q q q q q q q q q q qq q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q qq q q qq q q q qq qq q q q q q q q q q q q q q q q q q q q q q q q qqq q q qq q qq q q q q q q q q q q q q q qq q q q q qqq q q q q qq q qq q q q q q qq q qq qq q q q q q q q q qq q q q q q q q q q q q qq q q q q qq q q qq q q q q q q q q q q q q q q q q q q q q q q q qqqqq qq q q q qq q q q q q q qq q q q q q q qq q qq q q q q q q qq q qq q q q q q q q qq q q q q qq q q q q q q qq q q q q q q q qq q q q q q q q q q q q q q q q q q q q qq q q qq q q q q q q q q q q q qq q q q q q qq qq q q qq q q qq q q qq q q q q q q q q q q q q qq q q q q qq q q q qq q Cook's distance 0.5 Residuals vs Leverage 261 870 1200 52 / 90
  5. 5. © 2012 Heather Turner Multiple Regression Here we add key maternal characteristics. Dummy variables are automatically created for each level of race with white as the reference > (model2 <- lm(wt ~ gestation + wt.1 + ht + + parity + race, data = infant)) Call: lm(formula = wt ~ gestation + wt.1 + ht + parity + race, data = infant) Coefficients: (Intercept) gestation wt.1 ht -79.48027 0.44183 0.09081 0.99258 parity racemexican raceblack raceasian 0.86424 7.70791 -6.56544 -3.68222 racemixed race -0.56133 53 / 90
  6. 6. © 2012 Heather Turner Sequential Anova The significance of each addition to the model can be assessed using sequential anova, aka Type I ANOVA > anova(model2) Analysis of Variance Table Response: wt Df Sum Sq Mean Sq F value Pr(>F) gestation 1 66615 66615 255.9291 < 2.2e-16 *** wt.1 1 8382 8382 32.2039 1.751e-08 *** ht 1 6060 6060 23.2815 1.585e-06 *** parity 1 1567 1567 6.0189 0.0143 * race 4 10076 2519 9.6781 1.061e-07 *** Residuals 1163 302715 260 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ 54 / 90
  7. 7. © 2012 Heather Turner Type II Tests Type II tests are provide by the Anova function from the car package > library(car); Anova(model2) Anova Table (Type II tests) Response: wt Sum Sq Df F value Pr(>F) gestation 55567 1 213.4810 < 2.2e-16 *** wt.1 3052 1 11.7238 0.0006386 *** ht 5469 1 21.0132 5.054e-06 *** parity 2847 1 10.9393 0.0009702 *** race 10076 4 9.6781 1.061e-07 *** Residuals 302715 1163 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ 55 / 90

×