mussels<-read.csv(url("http://cknudson.com/data/mussels.csv"))
names(mussels)
##  [1] "GroupID"    "dry.mass"   "count"      "attached"   "lipid"     
##  [6] "protein"    "carbo"      "ash"        "Kcal"       "ammonia"   
## [11] "O2"         "AvgAmmonia" "AvgO2"      "AvgMass"
attach(mussels)

Using the mussels data we can create a linear model that will predict the average amount of ammonia based of the average mass of the mussel and if it is attached to a rock or amblema mussel.

mod=lm(AvgAmmonia ~ AvgMass+attached , mussels)
mod
## 
## Call:
## lm(formula = AvgAmmonia ~ AvgMass + attached, data = mussels)
## 
## Coefficients:
##  (Intercept)       AvgMass  attachedRock  
##     0.001140      0.239279     -0.002563
summary(mod)
## 
## Call:
## lm(formula = AvgAmmonia ~ AvgMass + attached, data = mussels)
## 
## 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

Now using the summary function we can conduct a hypothesis test. Our null hypothesis is that average mass coefficient is equal to zero. Our alternative hypothesis is that the average mass coefficient is not equal to zero. From above we can see that our test stata is 11.085 and our p-value is 3.86e-11. Because our p-value is much smaller than the alpha of .05 we can reject the null hypothesis in favor of the alternative. Thus, we can use the slope to say that for every additional amount of mass the amount of ammonia will increase by .2393 regardless of if it is attached to a rock or amblema mussel.

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

Using the above formula we can create a confidence interval for the regression coefficient of average mass. We are therefore 95% confident that for every addition of mass, the amount of amonia will increase from 1.948215e-01 to .2837372 per unit.

data = data.frame(AvgMass=.03, attached="Rock")
pred = predict(mod, data,interval = "predict")
pred
##           fit        lwr         upr
## 1 0.005755285 0.00354841 0.007962159
conf = predict(mod, data, interval = "confidence")
conf
##           fit         lwr         upr
## 1 0.005755285 0.005148067 0.006362502

Here are our prediction and confidence intervals for average ammonia while using if it is attached to a rock or amblema mussel. first we “create a new data frame to store specified values for our predictors. I choose .03 for average mass and rock. For our prediction interval we can say that we are 95% confident that when attached to a rock the average amount of ammonia will be between .00354841 and .007962159 units when the average mass is .03. Our point estimate is .005755.

For the confidence interval we are looking at the mean average ammonia when the average amount of mass is .03 and it is attached to a rock. We are 95% confident that when the average mass is .03 and the mussel is attacked to the rock, the mean average amount of ammonia will be between .005148067 and .006362502. We also get the same point estimate from our prediction interval.