suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(Rcmdr))
suppressPackageStartupMessages(library(mvabund))

Problem 1

fish <- read.csv("fish.csv", header=TRUE)
fish
##    density width flow temp
## 1        8  25.0  1.2 15.7
## 2       12  26.0  0.6 18.0
## 3       18  25.5  2.0 12.2
## 4       25  17.8  0.9 14.0
## 5       32  19.0  1.0 12.0
## 6       39  23.0  1.8 13.7
## 7       40  17.0  3.0 13.2
## 8       44  15.9  2.0 11.9
## 9       46  12.0  1.4 11.9
## 10      49  10.2  0.5 19.7
## 11      63  12.0  4.0 12.0
## 12      66  16.3  6.0  9.7
## 13      67   9.0  2.0 11.9
## 14      76   7.9  2.4 10.5
## 15      89   6.0  4.8 10.6
## 16     105   7.0  4.5 11.4
## 17     118   5.0  3.0 10.6
## 18     123   5.9  5.0  9.9
## 19     137   5.5  5.7 10.5
## 20     150   3.6  7.9  8.4
pairs(fish)

fish_standardized <- fish %>% mutate(
  density = (density - mean(density))/sd(density),
  width = (width - mean(width))/sd(width),
  flow = (flow - mean(flow))/sd(flow),
  temp = (temp - mean(temp))/sd(temp)
)

qqnorm(fish_standardized$density, xlab="Normal Scores", ylab="Standardized Residuals")
qqline(fish_standardized$density)

qqnorm(fish_standardized$width, xlab="Normal Scores", ylab="Standardized Residuals")
qqline(fish_standardized$width)

qqnorm(fish_standardized$flow, xlab="Normal Scores", ylab="Standardized Residuals")
qqline(fish_standardized$width)

qqnorm(fish_standardized$temp, xlab="Normal Scores", ylab="Standardized Residuals")
qqline(fish_standardized$width)

model1 <- lm(density~width+flow+temp, fish_standardized)
summary(model1)
## 
## Call:
## lm(formula = density ~ width + flow + temp, data = fish_standardized)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.45154 -0.17320 -0.05179  0.17136  0.52824 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.037e-16  7.099e-02   0.000  1.00000    
## width       -6.218e-01  9.356e-02  -6.646 5.62e-06 ***
## flow         4.377e-01  1.175e-01   3.725  0.00184 ** 
## temp        -2.974e-03  1.113e-01  -0.027  0.97902    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3175 on 16 degrees of freedom
## Multiple R-squared:  0.9151, Adjusted R-squared:  0.8992 
## F-statistic:  57.5 on 3 and 16 DF,  p-value: 8.651e-09
plot(model1$fitted, resid(model1),  ylab="Residuals", xlab="Fitted")

Problem 2

snails <- read.csv("snails.csv", header=TRUE)
snails
##    mass diameter
## 1  31.7     51.8
## 2  29.8     46.6
## 3  23.7     41.1
## 4  17.7     50.0
## 5  21.6     44.6
## 6  10.0     38.7
## 7  13.8     37.0
## 8   3.8     24.3
## 9  23.6     47.2
## 10 36.9     55.5
## 11 21.3     44.0
## 12 11.3     33.4
## 13 13.4     35.4
## 14 22.0     42.4
## 15 39.4     49.7
## 16 20.0     48.8
## 17 33.9     59.0
## 18 26.6     43.0
## 19  6.0     27.0
## 20  0.9     16.0
## 21 12.2     34.7
## 22  7.1     33.3
## 23  9.1     34.8
## 24 26.4     47.5
pairs(snails)

model2 <- lm(mass~diameter, snails)
summary(model2)
## 
## Call:
## lm(formula = mass ~ diameter, data = snails)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.9940 -1.7802 -0.5993  2.7303 11.9895 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -19.5648     4.2398  -4.615 0.000134 ***
## diameter      0.9452     0.1003   9.420 3.54e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.87 on 22 degrees of freedom
## Multiple R-squared:  0.8013, Adjusted R-squared:  0.7923 
## F-statistic: 88.73 on 1 and 22 DF,  p-value: 3.543e-09
plot(model2$fitted, resid(model2),  ylab="Residuals", xlab="Fitted")

model3 <- lm(diameter~mass, snails)
summary(model3)
## 
## Call:
## lm(formula = diameter ~ mass, data = snails)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -9.511 -2.979 -0.107  2.364 10.246 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  24.7479     1.9726   12.55 1.68e-11 ***
## mass          0.8478     0.0900    9.42 3.54e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.613 on 22 degrees of freedom
## Multiple R-squared:  0.8013, Adjusted R-squared:  0.7923 
## F-statistic: 88.73 on 1 and 22 DF,  p-value: 3.543e-09
plot(model3$fitted, resid(model3),  ylab="Residuals", xlab="Fitted")

Problem 3

voles <- read.csv("vole.csv", header=TRUE)
voles$vole <- NULL
#voles$mass <- factor(voles$mass)
pairs(voles)

# meanvar.plot(voles$rate~voles$mass)
# meanvar.plot(voles$log_rate~voles$mass)
leveneTest(rate~factor(voles$mass),voles)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  3  1.3364 0.2976
##       16
model4 <- lm(rate~mass,voles)
summary(model4)
## 
## Call:
## lm(formula = rate ~ mass, data = voles)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -56.846 -18.835  -2.575  16.263  66.477 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -41.3659    19.2337  -2.151   0.0453 *  
## mass          8.6493     0.8548  10.118 7.45e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 28.27 on 18 degrees of freedom
## Multiple R-squared:  0.8505, Adjusted R-squared:  0.8422 
## F-statistic: 102.4 on 1 and 18 DF,  p-value: 7.452e-09
plot(model4$fitted, resid(model4),  ylab="Residuals", xlab="Fitted")