Andrew Cooke
Andrew Cooke

Reputation: 61

How to nest effects within lmer?

I'm playing around with models.

If I run a basic GLM:

summary(glm(nfc~nform+p+namount+lime+pH+year, data=parkglm,family=gaussian()))

I get results that include year as an explanatory variable:

enter image description here

However, I need year to be nested in another factor, "period". There are 17 years, each year falls within one of two periods. I tried this using lme4 and lmer.

anova(lmer(nfc~nform+p+namount+lime+pH+(year|period), data=parkglm))

That gave me the below output which seems pretty reasonable, however you'll see that year or period aren't listed. I assume they've been controlled for, but I'm interested in their effects. I'm very iffy on nesting with in the lmer package.

lmer output

I appreciate there might be alot wrong going on here and apologies for that. If it's just a mess, fine, don't be afraid to say! I'm new to linear models in r and have struggled to find the info I need in a manner I can implement.

Here is a slight snapshot of the top of the data with types of data in each column.

data snap.

Upvotes: 0

Views: 494

Answers (1)

Jose
Jose

Reputation: 421

This formula is for a mixed-effects model with a random intercept for "period", assuming that each observation is grouped by that variable. Note that "year" was included as a fixed effect.

library(lme4)
summary(fm <- lmer(nfc ~ nform + p + namount + lime + pH + year + (1|period), data = parkglm))

However the model may not be appropriate given the low number of categories in the variable "period".

I suggest you take a look to some references about linear models and think about strategies to model your data, perhaps building categories or exploring different models. Two good manuscripts you may be interested:

Winter, B. (2013). Linear models and linear mixed effects models in R with linguistic applications. arXiv:1308.5499.

Douglas Bates, Martin Maechler, Ben Bolker, Steve Walker (2015). Fitting Linear Mixed-Effects Models Using lme4. Journal of Statistical Software, 67(1), 1-48. doi:10.18637/jss.v067.i01.

Upvotes: 2

Related Questions