For this learning log We will be finding a confidence interval for a few variables within a multiple linear regression.

These first lines get the data we will be looking into, and plot a linear model looking at the average ammonia produced, compared to average mass of the muscle, and if it is attached to a rock.

mass <- read.csv(url("http://cknudson.com/data/mussels.csv"))

mod <- lm(AvgAmmonia ~ AvgMass + attached, mass)

summary(mod)
## 
## Call:
## lm(formula = AvgAmmonia ~ AvgMass + attached, data = mass)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -2.019e-03 -5.240e-04 -5.959e-05  3.429e-04  2.526e-03 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   0.0011398  0.0005533   2.060     0.05 *  
## AvgMass       0.2392793  0.0215863  11.085 3.86e-11 ***
## attachedRock -0.0025629  0.0003931  -6.519 7.91e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.00103 on 25 degrees of freedom
## Multiple R-squared:  0.8574, Adjusted R-squared:  0.846 
## F-statistic: 75.18 on 2 and 25 DF,  p-value: 2.66e-11

H0: B1=0 Ha: B1!=0

Tnum <- coef(summary(mod))[2,1]/coef(summary(mod))[2,2]
Tnum
## [1] 11.08476

With a T of 11.08476, we can compute the probability of this occurring with 25 degrees of freedom (28-(2+1))

2*pt(Tnum, 25, lower.tail=FALSE)
## [1] 3.857471e-11

This helps show us that the likelihood of B1=0, is next to impossible.

the next few lines will work to set up a few confidence intervals.

confint(mod)
##                      2.5 %       97.5 %
## (Intercept)   1.999745e-07  0.002279427
## AvgMass       1.948215e-01  0.283737200
## attachedRock -3.372584e-03 -0.001753235

We are 95% confident that for any given rock, the average mass of muscles will be between .01948215 and .2837372.

The next few lines of code are for the prediction interval.

newdata <- data.frame( AvgMass=.03 , attached="Rock")
predy<- predict(mod,newdata,interval = "predict")
predy
##           fit        lwr         upr
## 1 0.005755285 0.00354841 0.007962159

This tells us that we are 95% confident that for any given rock, the average amount of ammonia will be between .00354841 and .007962159, given that the muscle is coming from a rock, and its mass is .03