Course Notes Home

Solution 1

Write a for loop to fit a linear regression model for each of the 7 test scores as the dependent variable including age,sex and collection site as covariates. Save the regression coefficient and P-value for the age covariate for each test. Use the following excerpt of code to create an empty matrix to fill with results.

output<-matrix(data = NA, nrow = 7, ncol = 2)
rownames(output)<-paste("Test", 1:7, sep = "")
colnames(output)<-c("Coeff", "P")
for(i in 1:7){
  model<-lm(dat[,i+6] ~ dat$Age + dat$Sex + dat$Collection)
  output[i,1]<-summary(model)$coefficients["dat$Age", 1]
  output[i,2]<-summary(model)$coefficients["dat$Age", 4]
}

## Scatterplot of Test 6 against Age
plot(dat$Age, dat$Test6, xlab = "Age", ylab = "Test6")
## add line of best fit
model<-lm(dat$Test6 ~ dat$Age)
abline(model)

## Add interaction
model<-lm(dat$Test6 ~ dat$Age + dat$Sex + dat$Collection + dat$Age*dat$Sex)
summary(model)$coefficients
##                               Estimate Std. Error      t value    Pr(>|t|)
## (Intercept)               -9.277156781  7.6492234 -1.212823348 0.237000639
## dat$Age                    0.928160546  0.2979008  3.115670099 0.004707037
## dat$SexM                   5.507979405 10.7910331  0.510421878 0.614418756
## dat$CollectionExeter      -0.761616990  2.1431129 -0.355378839 0.725410169
## dat$CollectionSouthampton  4.783366995  2.2928373  2.086221711 0.047759314
## dat$Age:dat$SexM          -0.004221547  0.4294155 -0.009830916 0.992237455
## Scatterplot of Test 6 against Age
plot(dat$Age, dat$Test6, xlab = "Age", ylab = "Test6", col = c("red", "blue")[dat$Sex])
## add line of best fit for females
model<-lm(dat$Test6 ~ dat$Age, subset = which(dat$Sex == "F"))
abline(model, col = "red")
model<-lm(dat$Test6 ~ dat$Age, subset = which(dat$Sex == "M"))
abline(model, col = "blue")
legend("bottomright", c("F", "M"), col = c("red", "blue"), lty = 1)