library(Matrix)
library(lme4)
## Warning: package 'lme4' was built under R version 3.5.2
library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 2.2.1     ✔ purrr   0.2.5
## ✔ tibble  1.4.2     ✔ dplyr   0.7.5
## ✔ tidyr   0.8.1     ✔ stringr 1.3.1
## ✔ readr   1.1.1     ✔ forcats 0.3.0
## ── Conflicts ───────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ tidyr::expand() masks Matrix::expand()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
Cows <- read.csv("~/Desktop/MATH239/Calf.csv",
                  header=TRUE)
View(Cows)
## Warning in system2("/usr/bin/otool", c("-L", shQuote(DSO)), stdout = TRUE):
## running command ''/usr/bin/otool' -L '/Library/Frameworks/R.framework/
## Resources/modules/R_de.so'' had status 1
#Part C fitting a fixed model 
modC<-lmer(Weight~1+(1|Sire), data=Cows)
modC
## Linear mixed model fit by REML ['lmerMod']
## Formula: Weight ~ 1 + (1 | Sire)
##    Data: Cows
## REML criterion at convergence: -22.8819
## Random effects:
##  Groups   Name        Std.Dev.
##  Sire     (Intercept) 0.07159 
##  Residual             0.12819 
## Number of obs: 24, groups:  Sire, 4
## Fixed Effects:
## (Intercept)  
##       1.075
#Part D Fitting linear model 
modD<-lm(Weight~1, data=Cows)
modD
## 
## Call:
## lm(formula = Weight ~ 1, data = Cows)
## 
## Coefficients:
## (Intercept)  
##       1.075
#Part E Compare models C & D through anova
anova(modC, modD)
## refitting model(s) with ML (instead of REML)
## Data: Cows
## Models:
## modD: Weight ~ 1
## modC: Weight ~ 1 + (1 | Sire)
##      Df     AIC     BIC logLik deviance  Chisq Chi Df Pr(>Chisq)
## modD  2 -22.275 -19.919 13.137  -26.275                         
## modC  3 -21.427 -17.892 13.713  -27.427 1.1518      1     0.2832
#Part G 95% confidence intervals
confint(modC)
## Computing profile confidence intervals ...
##                  2.5 %    97.5 %
## .sig01      0.00000000 0.1809616
## .sigma      0.09679291 0.1810922
## (Intercept) 0.97789364 1.1729396