Mid-Term

Question 1

Read .csv file and load ggplot

library("ggplot2")
midtermdata <- read.csv("midterm1data.csv")
head(midtermdata)
  X site pop sex age head_l skull_w total_l tail_l
1 1    1 Vic   m   8   94.1    60.4    89.0   36.0
2 2    1 Vic   f   6   92.5    57.6    91.5   36.5
3 3    1 Vic   f   6   94.0    60.0    95.5   39.0
4 4    1 Vic   f   6   93.2    57.1    92.0   38.0
5 5    1 Vic   f   2   91.5    56.3    85.5   36.0
6 6    1 Vic   f   1   93.1    54.8    90.5   35.5

Create the linear regression and histogram. I failed to create separate histograms (not sure if this is an issue), and did not have a line of best fit. I also incorrectly analyzed the histograms. I guess I don’t really understand how to analyze marginal histograms, because to me the histograms seem to indicate constance, with some right skew in skull width. I thought this would imply a linear model would work.

#lmmidterm <-lm(head_l ~ skull_w, data= midtermdata)
ggplot(data=midtermdata, aes(skull_w, head_l)) +
  geom_point()+
stat_smooth(method=lm)
`geom_smooth()` using formula = 'y ~ x'

Create the QQplot:this wasn’t necessary here

par(mfrow=c(1,2))
hist(midtermdata$skull_w)
hist(midtermdata$head_l)

b) Although the residual plot does show “some” quadratic behavior, a linear model still looks appropriate. I shouldn’t have used the term “quadratic”. Either way, I should have still created a polynomial model. However, I didn’t really understand creating polynomial models at this time. Homework 3 was helpful in this regard.

ggplot(midtermdata, aes(skull_w, log(head_l))) +
geom_point() +
stat_smooth(method='lm', formula = y ~poly(x,3))

I honestly ran out of time to do this part

lm1 <- lm(log(head_l) ~ poly(skull_w,3, raw=TRUE), data = midtermdata)
plot(predict(lm1), resid(lm1))
abline(h=0, col='red')

summary(lm1)

Call:
lm(formula = log(head_l) ~ poly(skull_w, 3, raw = TRUE), data = midtermdata)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.05700 -0.01468 -0.00197  0.01480  0.06848 

Coefficients:
                                Estimate Std. Error t value Pr(>|t|)  
(Intercept)                    1.121e+01  5.163e+00   2.171   0.0323 *
poly(skull_w, 3, raw = TRUE)1 -4.045e-01  2.641e-01  -1.531   0.1288  
poly(skull_w, 3, raw = TRUE)2  7.808e-03  4.489e-03   1.739   0.0851 .
poly(skull_w, 3, raw = TRUE)3 -4.852e-05  2.535e-05  -1.914   0.0584 .
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.02344 on 100 degrees of freedom
Multiple R-squared:  0.6442,    Adjusted R-squared:  0.6336 
F-statistic: 60.36 on 3 and 100 DF,  p-value: < 2.2e-16

It’s clear from the p-values that the third-order model and taking the log helps with the non-linearity d) The final model is predicted head length=46.19537+.81583(skull width). c) this is where I was supposed to insert the QQ plot*

par(mfrow=c(1,2))
qqnorm(resid(lm1))
qqline(resid(lm1), col='red')
hist(resid(lm1))

the QQ plot looks excellent. The historgram reflects the normal and bell-shaped nature of the distribution

#lm1 <- lm(head_l ~ skull_w, data=midterm)
#summary(lm1)

the above chunk is uneccessary now

*\[\ln(head_l) = \beta_{0}+\beta_{1}(skull_l)+\beta_{2}(skull_w{^2})+\epsilon\]

#predictdata<-data.frame(skull_w=55)
#pred <- predict(lm1, predictdata, interval = "prediction")

#Using R to create a 97% confidence interval for a 55mm skull width: I did not understand how to find or analuze high leverage points and/or outliers

plot(hatvalues(lm1), rstandard(lm1),
xlab = "Leverage", ylab = "Standardized Residuals")
n <- nrow(midtermdata)
abline(v=4/n, lty=2) # threshold for high leverage
abline(h=c(-2,2), lty=2) # threshold for outliers

I forgot the rule for outliers. I can see now there are 7 outliers, 3 of which are leverage points. There also 3 high leverage points which will need to be investigated

Problem 2

a)

summary(lm1)

Call:
lm(formula = log(head_l) ~ poly(skull_w, 3, raw = TRUE), data = midtermdata)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.05700 -0.01468 -0.00197  0.01480  0.06848 

Coefficients:
                                Estimate Std. Error t value Pr(>|t|)  
(Intercept)                    1.121e+01  5.163e+00   2.171   0.0323 *
poly(skull_w, 3, raw = TRUE)1 -4.045e-01  2.641e-01  -1.531   0.1288  
poly(skull_w, 3, raw = TRUE)2  7.808e-03  4.489e-03   1.739   0.0851 .
poly(skull_w, 3, raw = TRUE)3 -4.852e-05  2.535e-05  -1.914   0.0584 .
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.02344 on 100 degrees of freedom
Multiple R-squared:  0.6442,    Adjusted R-squared:  0.6336 
F-statistic: 60.36 on 3 and 100 DF,  p-value: < 2.2e-16

The adjusted R-squared value is 0.5004. This means that 50.04% of the head length can be explained by changes in the skull width.

The regression equation is head length=46.19537+.81583(skull width). That means that for every 1 mm increase in skull width, we expect to see a about a .81 increase in head length.

For a skull width of 55mm, the predicted value of head length would be 46.19537+.81583(55)=93.401867mm

The B_hat_0 value of my equation is 46.19537mm. This is not a meaningful value, as it would be very weird to encounter a possum with a head length of roughly 46 mm and a skull width of 0 mm. For some reason, this output is for the linear regression, even though the lm1 formula is clearly ##lm(log(head_l) ~ poly(skull_w,3, raw=TRUE), data = midtermdata). Therefore I will use the output of that summary to report and analyze R^2. The adjusted R-squared value is 0.6442 This means that about 64% of the head length can be explained by changes in skull width. The p-values are, respectively, \[\beta_0=0.1288, \beta_1=0.0851, and \beta_2=0.0584\], indicating that the higher order terms are marginally significant. Therefore, all three terms will be kept in the model. In my original answer, I did not analyze the value of \[\beta_0, \beta_1, and \beta_2,\] most likely because I was using a linear model.

b) The regression model is \[ln(\widehat(head_l))=-0.00004852(skull_w{^3})+0.007808(skull_w{^2})-.4045(skull_w))\]. Again, I was using a linear model.

c) The B_hat_0 value of my equation is 46.19537mm. This is not a meaningful value, as it would be very weird to encounter a possum with a head length of roughly 46 mm and a skull width of 0 mm. There is nothing wrong with this answer, although again, it uses the linear model. The updated answer would be \[e{^{11.121}}\], which still makes no sense in the context of the problem.

d) *Using the model, \[ln(\widehat{head_l}=-0.00004852(55{^3})+0.007808(55{^2})-.4045(55))\], \[ \widehat{head_l}= e^{(-0.00004852(55{^3})+0.007808(55{^2})-.4045(55))}\]=88.825mm. Using R to create a 97% confidence interval for a 55mm skull width:

e) *For this portion, I did not know how to insert a level into my predict function, and I also forgot to use the exp() function on my variable so I wouldn’t get the ln of skull_w.

predictdata<-data.frame("skull_w"=55)
pred <-predict(lm1, newdata= predictdata, interval="prediction", level = .97)
exp(pred)
       fit      lwr      upr
1 90.75449 86.15129 95.60365