Begin Mid-Term
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:
#lmmidterm <-lm(head_l ~ skull_w, data= midtermdata)
p <- ggplot (data= midtermdata, aes (skull_w, head_l)) +
geom_point ()
ggExtra:: ggMarginal (p, type= 'histogram' )
This data seems to follow a linear pattern, with some issues towards the larger head sizes. I will use a residual graph to confirm.
lm1 <- lm (head_l ~ skull_w, data= midtermdata)
summary (lm1)
Call:
lm(formula = head_l ~ skull_w, data = midtermdata)
Residuals:
Min 1Q Median 3Q Max
-8.9615 -1.3320 0.0588 1.6153 6.4444
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 46.19537 4.55355 10.14 <2e-16 ***
skull_w 0.81583 0.07993 10.21 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 2.526 on 102 degrees of freedom
Multiple R-squared: 0.5053, Adjusted R-squared: 0.5004
F-statistic: 104.2 on 1 and 102 DF, p-value: < 2.2e-16
Create the QQplot:
ggplot (midtermdata, aes (fitted (lm1), rstandard (lm1))) +
geom_point () +
geom_hline (yintercept = 0 ) +
xlab ("Fitted Values" ) + ylab ("Standardized Residuals" )
b) Although the residual plot does show “some” quadratic behavior, a linear model still looks appropriate.
d) The final model is predicted head length=46.19537+.81583(skull width).
{r}# identify outliers} #outliers <- which(abs(rstandard(lm1)) > 2)
{r}
lm1 <- lm(head_l ~ skull_w, data=midterm)
summary(lm1)
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.
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:
predictdata<- data.frame (skull_w= 55 )
predict (lm1, newdata= predictdata)