The R program
In this chapter we will mainly be using the lung data set so we will use the attach function.
attach(lung)
Regression equation from regressing ffev1a on fheight, bottom of p. 126.
First we create the variable ffev1a = ffev1/100.
ffev1a <- ffev1/100 lm1 <- lm(ffev1a ~ fheight, lung) summary(lm1) <output omitted> Coefficients: Value Std. Error t value Pr(>|t|) (Intercept) -4.0867 1.1520 -3.5475 0.0005 fheight 0.1181 0.0166 7.1065 0.0000
Descriptive statistics, middle of p. 127.
subset.male <- data.frame(fage, fheight, ffev1a) apply(subset.male, 2, mean) apply(subset.male, 2, stdev) apply(subset.male, 2, range) fage fheight ffev1a 40.13333 69.26 4.093267 fage fheight ffev1a 6.889995 2.779189 0.6507523 fage fheight ffev1a [1,] 26 61 2.50 [2,] 59 76 5.85
Regression equation from regressing ffev1a on fheight and fage, bottom p. 128.
lm2 <- lm(ffev1a ~ fage+fheight, lung) summary(lm2) Residuals: Min 1Q Median 3Q Max -1.347 -0.3414 0.009172 0.3717 1.419 Coefficients: Value Std. Error t value Pr(>|t|) (Intercept) -2.7607 1.1377 -2.4265 0.0165 fage -0.0266 0.0064 -4.1828 0.0000 fheight 0.1144 0.0158 7.2454 0.0000 Residual standard error: 0.5348 on 147 degrees of freedom Multiple R-Squared: 0.3337 F-statistic: 36.81 on 2 and 147 degrees of freedom, the p-value is 1.094e-013 Correlation of Coefficients: (Intercept) fage fage -0.2786 fheight -0.9738 0.0561
Covariance matrix, middle p. 133.
subset2 <- data.frame(fage, fheight, fweight, ffev1a) var(subset2, na.method="omit") fage fheight fweight ffev1a fage 47.472036 -1.0751678 -3.649217 -1.3876197 fheight -1.075168 7.7238926 34.695436 0.9122322 fweight -3.649217 34.6954362 573.797808 2.0671647 ffev1a -1.387620 0.9122322 2.067165 0.4234785
Correlation matrix, top p. 134.
cor(subset2, na.method="omit") fage fheight fweight ffev1a fage 1.00000000 -0.05614863 -0.02211064 -0.3094823 fheight -0.05614863 1.00000000 0.52116447 0.5043960 fweight -0.02211064 0.52116447 1.00000000 0.1326111 ffev1a -0.30948231 0.50439596 0.13261112 1.0000000
Table 7.1, p. 138.
Anova table corresponding to the regression of ffev1a on fheight and fage.
aov2 <- aov(ffev1a ~ fage+fheight, lung) summary(aov2) Df Sum of Sq Mean Sq F Value Pr(F) fage 1 6.04351 6.04351 21.13149 9.165832e-006 fheight 1 15.01346 15.01346 52.49544 2.300000e-011 Residuals 147 42.04133 0.28600
Table 7.5, p. 150.
Regressing fev1a on age and height for the subsets of males only.
apply(subset.male, 2, mean) apply(subset.male, 2, stdev) male.lm <- lm(ffev1a ~ fage+fheight, lung) summary(male.lm) fage fheight ffev1a 40.13333 69.26 4.093267 fage fheight ffev1a 6.889995 2.779189 0.6507523 Coefficients: Value Std. Error t value Pr(>|t|) (Intercept) -2.7607 1.1377 -2.4265 0.0165 fage -0.0266 0.0064 -4.1828 0.0000 fheight 0.1144 0.0158 7.2454 0.0000 Residual standard error: 0.5348 on 147 degrees of freedom Multiple R-Squared: 0.3337 F-statistic: 36.81 on 2 and 147 degrees of freedom, the p-value is 1.094e-013
Table 7.5, p. 150.
Regressing fev1a on age and height for the subsets of females only.
mfev1a <- mfev1/100 subset.female <- data.frame(mage, mheight, mfev1a) apply(subset.female, 2, mean) apply(subset.female, 2, stdev) female.lm <- lm(mfev1a ~ mage+mheight, lung) summary(female.lm) mage mheight mfev1a 37.56 64.09333 2.973133 mage mheight mfev1a 6.714184 2.469537 0.4874136 Coefficients: Value Std. Error t value Pr(>|t|) (Intercept) -2.2112 0.8961 -2.4676 0.0147 mage -0.0200 0.0050 -3.9630 0.0001 mheight 0.0926 0.0137 6.7565 0.0000 Residual standard error: 0.413 on 147 degrees of freedom Multiple R-Squared: 0.2915 F-statistic: 30.24 on 2 and 147 degrees of freedom, the p-value is 1e-011
Table 7.5, p. 150.
Regressing fev1a on age and height for both males and females using the data set lung.long.
apply(lung.long, 2, mean) apply(lung.long, 2, stdev) lm.all <- lm(fev1a ~ age+height, lung.long) summary(lm.all) age height fev1a 38.84667 66.67667 3.5332 age height fev1a 6.912484 3.685657 0.8025856 Coefficients: Value Std. Error t value Pr(>|t|) (Intercept) -6.7370 0.5633 -11.9601 0.0000 age -0.0186 0.0044 -4.1860 0.0000 height 0.1649 0.0083 19.7853 0.0000 Residual standard error: 0.5275 on 297 degrees of freedom Multiple R-Squared: 0.5709 F-statistic: 197.6 on 2 and 297 degrees of freedom, the p-value is 0
Unless you plan to continue to use the data set lung is a good idea to detach it.
detach(lung)