setwd("~/Dropbox/Fernando 2018/archives/data")
library("lme4", lib.loc="/Library/Frameworks/R.framework/Versions/3.5/Resources/library")
## Loading required package: Matrix
enemy<- read.table("invas_fam_out_2.csv", sep = ",", header = T)
attach(enemy)
damage<-as.factor(damage)
fam<-as.factor(fam)
##Modelo sin interacciónes
fit1<-lmer(ln_wt.total~damage*origen+(1|pop))
summary(fit1)
## Linear mixed model fit by REML ['lmerMod']
## Formula: ln_wt.total ~ damage * origen + (1 | pop)
##
## REML criterion at convergence: -168.9
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.6755 -0.6604 -0.1050 0.5993 3.8727
##
## Random effects:
## Groups Name Variance Std.Dev.
## pop (Intercept) 0.00194 0.04405
## Residual 0.03427 0.18512
## Number of obs: 359, groups: pop, 4
##
## Fixed effects:
## Estimate Std. Error t value
## (Intercept) 0.84941 0.03878 21.904
## damage1 -0.09855 0.03359 -2.934
## origenMEX 0.15075 0.05266 2.863
## damage1:origenMEX 0.03139 0.04132 0.760
##
## Correlation of Fixed Effects:
## (Intr) damag1 orgMEX
## damage1 -0.405
## origenMEX -0.736 0.298
## dmg1:rgnMEX 0.329 -0.813 -0.380
##Probando efectos fijos (damage) con la correción Kenward-Roger para datos desbalanceados
library(pbkrtest)
modtodos<-lmer(ln_wt.total~damage+origen+(1|pop)+damage:origen, REML = F)
modsindamage<-lmer(ln_wt.total~origen+(1|pop), REML = F)
p.damage<-KRmodcomp(modtodos,modsindamage)
p.damage
## F-test with Kenward-Roger approximation; computing time: 0.13 sec.
## large : ln_wt.total ~ damage + origen + (1 | pop) + damage:origen
## small : ln_wt.total ~ origen + (1 | pop)
## stat ndf ddf F.scaling p.value
## Ftest 8.1984 2.0000 353.0120 1 0.0003309 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
modsinorigen<-lmer(ln_wt.total~damage+(1|pop), REML = F)
p.origen<-KRmodcomp(modtodos,modsinorigen)
p.origen
## F-test with Kenward-Roger approximation; computing time: 0.08 sec.
## large : ln_wt.total ~ damage + origen + (1 | pop) + damage:origen
## small : ln_wt.total ~ damage + (1 | pop)
## stat ndf ddf F.scaling p.value
## Ftest 5.3306 2.0000 4.6722 0.87659 0.06227 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
modsininter<-lmer(ln_wt.total~damage+origen+(1|pop), REML=F)
p.inter<-KRmodcomp(modtodos,modsininter)
p.inter
## F-test with Kenward-Roger approximation; computing time: 0.08 sec.
## large : ln_wt.total ~ damage + origen + (1 | pop) + damage:origen
## small : ln_wt.total ~ damage + origen + (1 | pop)
## stat ndf ddf F.scaling p.value
## Ftest 0.5772 1.0000 353.0121 1 0.4479
##Probando efectos aleatorios
library(lmerTest)
##
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
##
## lmer
## The following object is masked from 'package:stats':
##
## step
ranova(fit1)
## ANOVA-like table for random-effects: Single term deletions
##
## Model:
## ln_wt.total ~ damage + origen + (1 | pop) + damage:origen
## npar logLik AIC LRT Df Pr(>Chisq)
## <none> 6 84.429 -156.86
## (1 | pop) 5 80.102 -150.20 8.6537 1 0.003264 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
detach("package:lmerTest", unload=TRUE)
#Finalmente el diagnostico de residuales del modelo...
plot(residuals(fit1)~predict(fit1,type="link"), xlab=expression(hat(eta)), ylab="Deviance residuals")

#...y los intervalos de confianza para las familias y poblaciones
library(lattice)
ranef(fit1)
## $pop
## (Intercept)
## morelos -0.039205300
## teotihuacan 0.039205300
## valdeflores 0.007345003
## zubia -0.007345003
dput(cl<-ranef(fit1,condVar=T))
## structure(list(pop = structure(list(`(Intercept)` = c(-0.0392052999801728,
## 0.0392052999801853, 0.00734500302944189, -0.00734500302944506
## )), class = "data.frame", row.names = c("morelos", "teotihuacan",
## "valdeflores", "zubia"), postVar = structure(c(0.000272719313141606,
## 0.000233670062193092, 0.000386527112296837, 0.000499112774844677
## ), .Dim = c(1L, 1L, 4L)))), class = "ranef.mer")
dotplot(cl)
## $pop
