Skip to content

lmer predicted and fitted values differ

5 messages · ONKELINX, Thierry, Amy Wade

#
Dear Amy,

The functions that you used take only the fixed effects into account.
While fitted() takes both the fixed and the random effects into account.

What do you want to predict? The 'average' data (all random effect
levels = 0)? Data for existing levels of the random effects? Data for
new levels of the random effects?

Best regards,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
#
Theirry,

Thanks for that clarification. I would like to predict data for the existing
levels of the random effects.


Many thanks,
Amy

P.S. not sure how to continue a thread - hope this works....





.......................................
Amy S. I. Wade
Centre for Agri-Environmental Research
University of Reading





-----Original Message-----
From: ONKELINX, Thierry [mailto:Thierry.ONKELINX at inbo.be] 
Sent: 24 November 2010 12:47
To: Amy Wade; r-sig-mixed-models at r-project.org
Subject: RE: [R-sig-ME] lmer predicted and fitted values differ

Dear Amy,

The functions that you used take only the fixed effects into account.
While fitted() takes both the fixed and the random effects into account.

What do you want to predict? The 'average' data (all random effect
levels = 0)? Data for existing levels of the random effects? Data for
new levels of the random effects?

Best regards,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
#
Dear Amy,

Here is a solution. It should work with nested random effects. Untested
with random slopes and crossed random effects.

Best regards,

Thierry

library(lme4)
cbpp$Obs <- factor(seq_len(nrow(cbpp)))
(gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 |
herd/Obs), family = binomial, data = cbpp))
str(gm1)

predict.lmerBin <- function(object, X, Zt){
	if(missing(X)){
		X <- object at X
	}
	b <- fixef(object)
	if(missing(Zt)){
		Zt <- as.matrix(object at Zt)
	}
	z <- unlist(ranef(object))
	plogis(X %*% b + t(Zt) %*% z)
}

all.equal(as.vector(predict.lmerBin(gm1)), fitted(gm1))

PS Replying to all should keep the thread intact.

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
#
Thierry,

Your function did exactly what I wanted. Very much appreciated.

All the best,
Amy


.....................................................
Amy S. I. Wade
Centre for Agri-Environmental Research
University of Reading




-----Original Message-----
From: ONKELINX, Thierry [mailto:Thierry.ONKELINX at inbo.be] 
Sent: 24 November 2010 14:16
To: Amy Wade; r-sig-mixed-models at r-project.org
Subject: RE: lmer predicted and fitted values differ

Dear Amy,

Here is a solution. It should work with nested random effects. Untested
with random slopes and crossed random effects.

Best regards,

Thierry

library(lme4)
cbpp$Obs <- factor(seq_len(nrow(cbpp)))
(gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 |
herd/Obs), family = binomial, data = cbpp))
str(gm1)

predict.lmerBin <- function(object, X, Zt){
	if(missing(X)){
		X <- object at X
	}
	b <- fixef(object)
	if(missing(Zt)){
		Zt <- as.matrix(object at Zt)
	}
	z <- unlist(ranef(object))
	plogis(X %*% b + t(Zt) %*% z)
}

all.equal(as.vector(predict.lmerBin(gm1)), fitted(gm1))

PS Replying to all should keep the thread intact.

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey