#obtaining dataset
fruitfly<-read.csv("fruitfly.csv")
#summary stats DO
summary(fruitfly)
## No type lifespan thorax sleep
## Min. : 1 Min. :1 Min. :16.00 Min. :0.640 Min. : 1.00
## 1st Qu.: 7 1st Qu.:2 1st Qu.:46.00 1st Qu.:0.760 1st Qu.:13.00
## Median :13 Median :3 Median :58.00 Median :0.840 Median :20.00
## Mean :13 Mean :3 Mean :57.44 Mean :0.821 Mean :23.46
## 3rd Qu.:19 3rd Qu.:4 3rd Qu.:70.00 3rd Qu.:0.880 3rd Qu.:29.00
## Max. :25 Max. :5 Max. :97.00 Max. :0.940 Max. :83.00
#distribution of lifespan
hist(fruitfly$lifespan)
As we can see, the data set that was imported has 5 variables: No, type, lifespan, thorax, and sleep. A variable that will be of particular focus for this homework is lifespan. After creating a histogram, we can see that the lifespan of the fruitfly follows a normal distribution.
y<-fruitfly$lifespan
x<-fruitfly$thorax
plot(lifespan ~ thorax, data = fruitfly)
cor(x, y)
## [1] 0.6364835
In the scatterplot, we can see that as lifespan increases, thorax increases, suggesting that these variables have a positive linear relationship . This is confirmed by calculating the correlation coeficient. There is a positive linear relationship (r = .63) between lifespan and thorax.
lin_reg <- lm(y ~ x)
lin_reg
##
## Call:
## lm(formula = y ~ x)
##
## Coefficients:
## (Intercept) x
## -61.05 144.33
#confirming output makes sense by making plot
# plot(fruitfly$lifespan~fruitfly$thorax)
# abline(lm.reg)
40.51% of the variability in lifespan can be explain by thorax size. The slope of this regression is 144.3. This means that for any 1 mm increase in thorax, the fruitfly lifespan is expected to increase by 144.3 days. This slope does not make complete sense, since the oldest fruitfly in the dataset has a thorax of 0.94. However, considering the y intercept, which is the number that represents the lifespan of a fruitfly that is 0 days old, is negative (which doesn’t make sense as there are no negative mm), the equation must consider this and compensate accordingly.
#manual way of doing T-test:
#Ho: $\beta_1$=0 vs Ha: $\beta_1 \neq$ 0
# names(lin_reg)
# lin_reg$coefficients
# lin_reg$coefficients[2]
# tteststatistic <- (lin_reg$coefficient[2]-0)/15.77
# #lower because we are doing two-tailed test, multiplying by two after
# pvalue<-2*pt(tteststatistic,123,lower.tail = T)
# pvalue
#Getting the p-value through summary
summary(lin_reg)
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -28.415 -9.961 1.132 9.265 36.812
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -61.05 13.00 -4.695 7.0e-06 ***
## x 144.33 15.77 9.152 1.5e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 13.6 on 123 degrees of freedom
## Multiple R-squared: 0.4051, Adjusted R-squared: 0.4003
## F-statistic: 83.76 on 1 and 123 DF, p-value: 1.497e-15
The linear relationship between lifespan and thorax is significant. Thorax length predicted lifespan, R^2 = .41, F(1,123) = 83.76, p < .005.
#confidence interval
y<-fruitfly$lifespan
x<-fruitfly$thorax
lin_reg = lm(y ~ x)
#method 1
#Remember: Has to add up to 1
critical_value = qt(0.05,123,lower.tail = F)
#check if its or y error
U<- lin_reg$coefficients[2] + critical_value*15.77
L<- lin_reg$coefficients[2] - critical_value*15.77
confidence_interval_west = paste("(",L,",",U, ")")
print(confidence_interval_west)
## [1] "( 118.196906057843 , 170.469296020757 )"
#method 2
confint(lin_reg, level = 0.9)
## 5 % 95 %
## (Intercept) -82.60361 -39.4998
## x 118.19616 170.4700
It can be said with 90% confidence that the true value of the slope of the fitted model lies between 118.19 and 170.47. This means that for any 1 mm increase in thorax, the fruitfly lifespan is expected to increase by a value in that range of numbers. Once again, this prediction does not seem to be applicable, due to the existing lifespan values on the dataset but it must be accounting to the negative y-intercept.
print("individual fruitfly: ")
## [1] "individual fruitfly: "
#prediction for individual fruitfly
predict(lm(y ~ x), newdata = data.frame(x = 0.8), se.fit = TRUE, interval = "prediction", level = 0.95)
## $fit
## fit lwr upr
## 1 54.41478 27.37542 81.45414
##
## $se.fit
## [1] 1.260689
##
## $df
## [1] 123
##
## $residual.scale
## [1] 13.60182
print("average lifespan: ")
## [1] "average lifespan: "
#prediction for an average lifespan of a fruitfly
predict(lm(y ~ x), newdata = data.frame(x = 0.8), se.fit = TRUE, interval = "confidence", level = 0.95)
## $fit
## fit lwr upr
## 1 54.41478 51.91932 56.91024
##
## $se.fit
## [1] 1.260689
##
## $df
## [1] 123
##
## $residual.scale
## [1] 13.60182
The predicted value of lifespan for a fruitfly with thorax of 0.8 mm is 54.41 days.
The lifespan of an individual fruitfly with a thorax of 0.8 mm ranges between the value of 27.37 days and 81.45 days in 90% of predictive intervals.
The average lifespan of a fruitfly with a thorax of 0.8 mm ranges between the value of 51.92 days and 56.91 in 90% of confidence intervals.
new<-data.frame(x=seq(0.5,0.96,0.005))
prep.m<-predict(lm(y~x), newdata = new, interval = "confidence")
prep.p<-predict(lm(y~x), newdata = new, interval = "prediction")
#cbind(prep.m, prep.p)
matplot(new$x, cbind(prep.m, prep.p[,-1]),
lty = c(1,2,2,3,3), type = "l",
col = c("black", "blue", "blue", "red", "red"),
ylab = "predicted lifespan",
xlab = "thorax value")
In this graph, we can see the linear regression line in black, accompanied by two sets of dashed lines: In blue, we have the confidence interval and in red, we have the predictive intervals.
As we can see in the chart, the confidence bands for the confidence intervals (blue) are closer to the linear regression line than those for the predictive intervals (red). This is because in predictive intervals there are two sources of uncertainty: The uncertainty of group mean, and the variability of individual outcome around group mean (also referred to as random error). However, in confidence intervals there is only one source of uncertainty, the group mean uncertainty.