library(dplyr)
library(ggplot2)
library(ggfortify)
growth.moo <- read.csv('/Users/gerrisummers/Documents/Biostats4259a/Data/growth.csv')
glimpse(growth.moo)
## Rows: 48
## Columns: 3
## $ supplement <chr> "supergain", "supergain", "supergain", "supergain", "contro…
## $ diet <chr> "wheat", "wheat", "wheat", "wheat", "wheat", "wheat", "whea…
## $ gain <dbl> 17.37125, 16.81489, 18.08184, 15.78175, 17.70656, 18.22717,…
Loads the libraries and the data, and glimpse() shows its structure.
growth.moo <- mutate(growth.moo,
diet = factor(diet),
supplement = factor(supplement))
levels(growth.moo$diet)
## [1] "barley" "oats" "wheat"
levels(growth.moo$supplement)
## [1] "agrimore" "control" "supergain" "supersupp"
growth.moo <-
mutate(growth.moo,
supplement = relevel(supplement, ref = "control"))
levels(growth.moo$supplement)
## [1] "control" "agrimore" "supergain" "supersupp"
levels(growth.moo$diet)
## [1] "barley" "oats" "wheat"
growth.moo %>%
group_by(diet, supplement) %>%
summarise(n = n())
## # A tibble: 12 × 3
## # Groups: diet [3]
## diet supplement n
## <fct> <fct> <int>
## 1 barley control 4
## 2 barley agrimore 4
## 3 barley supergain 4
## 4 barley supersupp 4
## 5 oats control 4
## 6 oats agrimore 4
## 7 oats supergain 4
## 8 oats supersupp 4
## 9 wheat control 4
## 10 wheat agrimore 4
## 11 wheat supergain 4
## 12 wheat supersupp 4
Counts cows in each diet × supplement group, and it is balanced.
growth.moo %>%
mutate(order = row_number()) %>%
ggplot(aes(x = gain, y = order)) +
geom_point() +
theme_bw()
Using a Cleveland dotplot there is no evidence of outliers in weight
gain
ggplot(growth.moo, aes(x = supplement, y = gain, fill = diet)) +
geom_boxplot() +
theme_bw()
The spread of weight gain seems similar across groups, but barley groups
appear slightly more variable to the others
sumMoo <- growth.moo %>%
group_by(diet, supplement) %>%
summarise(
meanGrow = mean(gain),
seGrow = sd(gain)/sqrt(n())
)
sumMoo
## # A tibble: 12 × 4
## # Groups: diet [3]
## diet supplement meanGrow seGrow
## <fct> <fct> <dbl> <dbl>
## 1 barley control 23.3 0.703
## 2 barley agrimore 26.3 0.919
## 3 barley supergain 22.5 0.771
## 4 barley supersupp 25.6 1.06
## 5 oats control 20.5 0.506
## 6 oats agrimore 23.3 0.613
## 7 oats supergain 19.7 0.349
## 8 oats supersupp 21.9 0.413
## 9 wheat control 17.4 0.460
## 10 wheat agrimore 19.6 0.710
## 11 wheat supergain 17.0 0.485
## 12 wheat supersupp 19.7 0.475
ggplot(sumMoo, aes(x = supplement, y = meanGrow, colour = diet, group = diet)) +
geom_point() +
geom_line() +
geom_errorbar(aes(ymin = meanGrow - seGrow,
ymax = meanGrow + seGrow), width = 0.1) +
theme_bw()
Barley gives the highest weight gain and wheat the lowest under every
supplement, and the lines are roughly parallel, which suggests there is
no interaction between diet and supplement.
model_cow <- lm(gain ~ diet * supplement, data = growth.moo)
autoplot(model_cow, smooth.colour = NA)
The residuals are evenly scattered around zero, the points fall mostly
close to the line in the Q-Q plot with slight curving at the tails, and
there is no clear fan shape in the Scale-Location plot (barley groups at
high fitted values are slightly more spread, as in the boxplot), so the
assumptions of the model appear reasonably met.
anova(model_cow)
## Analysis of Variance Table
##
## Response: gain
## Df Sum Sq Mean Sq F value Pr(>F)
## diet 2 287.171 143.586 83.5201 2.999e-14 ***
## supplement 3 91.881 30.627 17.8150 2.952e-07 ***
## diet:supplement 6 3.406 0.568 0.3302 0.9166
## Residuals 36 61.890 1.719
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
After reviewing the ANOVA table, the interaction between diet and supplement on the weight gain of the cows is not significant (F=0.3302, df=6,36, p=0.92). We fail to reject the null hypothesis, as there is no evidence of an interaction between diet and supplement; the effect of supplement on weight gain does not depend on diet. Diet has a significant effect on weight gain (F = 83.52, df = 2, 36, p < 0.001), and supplement has a significant effect on weight gain (F = 17.82, df = 3, 36, p < 0.001).
summary(model_cow)
##
## Call:
## lm(formula = gain ~ diet * supplement, data = growth.moo)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.48756 -1.00368 -0.07452 1.03496 2.68069
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 23.2966499 0.6555863 35.536 < 2e-16 ***
## dietoats -2.8029851 0.9271390 -3.023 0.00459 **
## dietwheat -5.8911317 0.9271390 -6.354 2.34e-07 ***
## supplementagrimore 3.0518277 0.9271390 3.292 0.00224 **
## supplementsupergain -0.8305263 0.9271390 -0.896 0.37631
## supplementsupersupp 2.2786527 0.9271390 2.458 0.01893 *
## dietoats:supplementagrimore -0.2471088 1.3111726 -0.188 0.85157
## dietwheat:supplementagrimore -0.8182729 1.3111726 -0.624 0.53651
## dietoats:supplementsupergain -0.0001351 1.3111726 0.000 0.99992
## dietwheat:supplementsupergain 0.4374395 1.3111726 0.334 0.74060
## dietoats:supplementsupersupp -0.9120830 1.3111726 -0.696 0.49113
## dietwheat:supplementsupersupp -0.0158299 1.3111726 -0.012 0.99043
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.311 on 36 degrees of freedom
## Multiple R-squared: 0.8607, Adjusted R-squared: 0.8182
## F-statistic: 20.22 on 11 and 36 DF, p-value: 3.295e-12
The intercept is the mean weight gain for cows on barley with the control supplement, and every other coefficient is the difference from that reference group.
limp <- read.csv('/Users/gerrisummers/Documents/Biostats4259a/Data/limpet.csv')
limp <- mutate(limp,
SEASON = factor(SEASON))
glimpse(limp)
## Rows: 24
## Columns: 3
## $ DENSITY <int> 8, 8, 8, 8, 8, 8, 15, 15, 15, 15, 15, 15, 30, 30, 30, 30, 30, …
## $ SEASON <fct> spring, spring, spring, summer, summer, summer, spring, spring…
## $ EGGS <dbl> 2.875, 2.625, 1.750, 2.125, 1.500, 1.875, 2.600, 1.866, 2.066,…
limp %>%
group_by(SEASON) %>%
summarise(n = n())
## # A tibble: 2 × 2
## SEASON n
## <fct> <int>
## 1 spring 12
## 2 summer 12
Counts match (12) and data is balanced
limp %>%
mutate(order = row_number()) %>%
ggplot(aes(x = EGGS, y = order)) +
geom_point() +
theme_bw()
limp %>%
mutate(order = row_number()) %>%
ggplot(aes(x = DENSITY, y = order)) +
geom_point() +
theme_bw()
No outliers detected in dataset.
ggplot(limp, aes(x = DENSITY, y = EGGS, colour = SEASON)) + geom_point() +
scale_colour_manual(values = c(spring="green",summer="red")) +
theme_bw()
Egg production decreases as limpet density increases, spring limpets
produce more eggs, and the two seasons appear to have similar
slopes.
limp.mod <- lm(EGGS ~ DENSITY * SEASON, data = limp)
names(limp.mod)
## [1] "coefficients" "residuals" "effects" "rank"
## [5] "fitted.values" "assign" "qr" "df.residual"
## [9] "contrasts" "xlevels" "call" "terms"
## [13] "model"
autoplot(limp.mod, smooth.colour = NA)
The residuals are evenly scattered around zero, the points fall close to
the line in the Q-Q plot, there is no fan shape in the Scale-Location
plot, and no points have unusually high leverage, so the assumptions of
the model appear to be met.
anova(limp.mod)
## Analysis of Variance Table
##
## Response: EGGS
## Df Sum Sq Mean Sq F value Pr(>F)
## DENSITY 1 5.0241 5.0241 30.1971 2.226e-05 ***
## SEASON 1 3.2502 3.2502 19.5350 0.0002637 ***
## DENSITY:SEASON 1 0.0118 0.0118 0.0711 0.7925333
## Residuals 20 3.3275 0.1664
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
There is no evidence of an interaction between density and season (F = 0.0711, df = 1, 20, p = 0.79). Therefore we fail to reject the null hypothesis; the effect of density on egg production in limpets does not depend on the season. Density has a significant effect on egg production (F = 30.20, df = 1, 20, p < 0.001), and season has a significant effect on egg production (F = 19.54, df = 1, 20, p < 0.001).
summary(limp.mod)
##
## Call:
## lm(formula = EGGS ~ DENSITY * SEASON, data = limp)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.65468 -0.25021 -0.03318 0.28335 0.57532
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.664166 0.234118 11.380 3.45e-10 ***
## DENSITY -0.033650 0.008259 -4.074 0.000591 ***
## SEASONsummer -0.812282 0.331092 -2.453 0.023450 *
## DENSITY:SEASONsummer 0.003114 0.011680 0.267 0.792533
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4079 on 20 degrees of freedom
## Multiple R-squared: 0.7135, Adjusted R-squared: 0.6705
## F-statistic: 16.6 on 3 and 20 DF, p-value: 1.186e-05
The intercept and DENSITY coefficient are the intercept and slope for spring, SEASONsummer is how much the summer intercept differs from spring, and DENSITY:SEASONsummer is how much the summer slope differs from spring.
expand.grid(FRIST = c("A", "B"), SECOND = c(1,2))
## FRIST SECOND
## 1 A 1
## 2 B 1
## 3 A 2
## 4 B 2
predict(limp.mod)
## 1 2 3 4 5 6 7 8
## 2.3949692 2.3949692 2.3949692 1.6075953 1.6075953 1.6075953 2.1594217 2.1594217
## 9 10 11 12 13 14 15 16
## 2.1594217 1.3938428 1.3938428 1.3938428 1.6546769 1.6546769 1.6546769 0.9358016
## 17 18 19 20 21 22 23 24
## 0.9358016 0.9358016 1.1499321 1.1499321 1.1499321 0.4777604 0.4777604 0.4777604
new.x <- expand.grid(DENSITY =
seq(from = 8, to =45, length.out = 10), SEASON = levels(limp$SEASON))
head(new.x)
## DENSITY SEASON
## 1 8.00000 spring
## 2 12.11111 spring
## 3 16.22222 spring
## 4 20.33333 spring
## 5 24.44444 spring
## 6 28.55556 spring
new.y <- predict(limp.mod, newdata=new.x,
interval = 'confidence')
head(new.y)
## fit lwr upr
## 1 2.394969 2.019285 2.770654
## 2 2.256632 1.931230 2.582034
## 3 2.118294 1.834274 2.402315
## 4 1.979957 1.724062 2.235852
## 5 1.841619 1.595998 2.087241
## 6 1.703282 1.447918 1.958646
addThese <- data.frame(new.x, new.y)
addThese <- rename(addThese, EGGS= fit)
head(addThese)
## DENSITY SEASON EGGS lwr upr
## 1 8.00000 spring 2.394969 2.019285 2.770654
## 2 12.11111 spring 2.256632 1.931230 2.582034
## 3 16.22222 spring 2.118294 1.834274 2.402315
## 4 20.33333 spring 1.979957 1.724062 2.235852
## 5 24.44444 spring 1.841619 1.595998 2.087241
## 6 28.55556 spring 1.703282 1.447918 1.958646
# new.w using density to establish a mean
new.x <- expand.grid(DENSITY=mean(limp$DENSITY),
SEASON = levels(limp$SEASON))
# predicting
predEgg <- predict(limp.mod, newdata = new.x)
# Housekeeping
EggAtMeanDens <- data.frame(new.x, predEgg)
head(EggAtMeanDens)
## DENSITY SEASON predEgg
## 1 24.5 spring 1.83975
## 2 24.5 summer 1.10375
At the mean limpet density (24.5), the model predicts 1.84 eggs in spring and 1.10 eggs in summer, so spring limpets produce more eggs than summer limpets at the same density.
ggplot(limp, aes(x = DENSITY, y = EGGS, colour = SEASON)) + geom_point(size =5) + geom_smooth(data = addThese, aes(ymin = lwr, ymax = upr, fill = SEASON), stat = 'identity') + scale_colour_manual(values = c(spring="green",summer="red")) +
scale_fill_manual(values = c(spring="green",summer="red"))+
theme_bw()
The fitted lines show egg production decreasing with density in both
seasons, with parallel slopes (no interaction) and spring consistently
higher than summer.