knitr::opts_chunk$set(echo = TRUE)

Preliminaires

Load packages

Load data

milk_fat <- data("milk_fat")

library(ggplot2)

fat mass

log10 coef predict

range data.frame

Questions * Write the regression equation for the equation of the log10-log10 regression line line

Which plot provides information about... * ... homogeneity of variance? * ... normality of residuals? * ... outliers/influential points?

ANSWERS

Model

Regression: fat ~ body size

The author's of the milk paper indicate that there are typically correlations between the size of mammal and fat content. They note:

"Within particular phylogenetic groups, there are significant negative relationships between body mass & ... fat ... (Merchant et al. 1989; Derrickson et al 1996; Hinde & Milligan 2011). Species with small body masses are expected to produce more concentrated and energetically dense milk because they have higher mass-specific metabolic demands and reduced digestive capacity due to smaller GI tracts (Blaxter 1961). Thus, small-bodied species should be incapable of ingesting greater quantities of milk to meet metabolic and nutritional demands & should instead require more concentrated and energy dense milk (Blaxter 1961)." (Skibiel et al 201x. pg yy)

References

Derrickson 1992. Comparative reproductive strategies of altricial & precocial eutherian mammals. Functional Ecology, 6, 57-65. Derrickson et al. 1996. Milk composition of two precocial, arid-dwelling rodents, Kerodon rupestris and Acomys cahirinus. Physiological Zoology, 69, 1402-1418.

Plot fat ~ body size

qplot(y=fat.log10,
      x=mass.log10, 
      data = dat2)

The author's model: log(fat) vs log(mass)

Regression fat % against mass of animal

log.log.mass <- lm(fat.log10 ~ mass.log10, data = dat2)
summary(log.log.mass)

QUESTION:

My model logit(fat) ~ log(mass)

Fit a model to logit transformed data

logit.log.mass <- lm(fat.logit ~ mass.log10, data = dat2)

summary(logit.log.mass)

using coef()

Compare just at the coefficients using coef()

coef(log.log.mass)
coef(logit.log.mass)

Look at the p values for the two slopes

summary(log.log.mass)$coefficients
summary(logit.log.mass)$coefficients

The p value is ~0.4 for the log-log and ~0.2 for logit-log.

Look at the residuals of the log-log model

par(mfrow = c(2,2), 
    mar = c(4,4,2,1))
plot(log.log.mass)

QUESTIONS

Which plot provides information about... ... homogeneity of variance? ... normality of residuals? * ... outliers/influential points?

ANSWERS

Some thoughts:

Look at the residuals of the logit-log model

par(mfrow = c(2,2), 
    mar = c(4,4,2,1))
plot(logit.log.mass)

On (not) Comparing models with different transformations of the response variable

Plot model

Get predictions from model

Get predictions

Get the predictions with predict()

y.hat <- predict(log.log.mass)

Add prediction to the dataframe

dat2$y.hat <- y.hat

Plot predictions

Plot predictions with points()

par(mfrow = c(1,1))

#plot raw dat
plot(fat.log10 ~ mass.log10, data = dat2)

#plot predicitons as points
points(y.hat ~ mass.log10, data  = dat2, col = 2)

We can plot the predictions as a straight line like this by using the "type = 'l'" argument in points().

#plot raw dat
plot(fat.log10 ~ mass.log10, data = dat2)

#plot predicitons as points
points(y.hat ~ mass.log10, 
       data  = dat2, 
       col = 2, 
       type = "l")

Plot predictions from a defined dataset

1st, get the range of the x variable

#get the range of x variable
observed.range <- range(dat2$mass.log10)

observed.range

Make into a dataframe

#make new dataframe
newdat <- data.frame(mass.log10 = observed.range)

newdat

Using predict() with newdata

y.hat2 <- predict(log.log.mass, 
                  newdat = newdat)

y.hat2

Add the y.hat predictions to our new dataframe

newdat$y.hat2 <- y.hat2

newdat

Plot raw data w/ line defined by the predictions

#plot raw dat
plot(fat.log10 ~ mass.log10, data = dat2)

#plot predicitons as points
points(y.hat2 ~ mass.log10, 
       data  = newdat, 
       col = 2, 
       type = "l")

Plotting simple model output with abline()

#plot raw dat
plot(fat.log10 ~ mass.log10, 
     data = dat2)

#plot model w/abline
abline(log.log.mass, 
       col = 2)


brouwern/mammalsmilk documentation built on May 17, 2019, 10:38 a.m.