"effects" package with "lme4"

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

"effects" package with "lme4"

Axel Urbiz
Hello John and others,

I’d appreciate your help as I’m trying to plot the effect of predictor “Days” on Reaction by Subject. I’m only getting one plot in the example below.

### Start example

library(lme4)
library(splines)
data("sleepstudy")

fm1 <- lmer(Reaction ~ ns(Days, 3) + (ns(Days, 3) | Subject), sleepstudy)
coef(fm1)
plot(allEffects(fm1))

### End example

Thanks,
Axel.
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: "effects" package with "lme4"

Fox, John
Dear Axel,

There only one fixed effect in the model, ns(Days, 3), so I don't know what you expected.

Best,
 John
--------------------------------------
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
Web: socialsciences.mcmaster.ca/jfox/



> -----Original Message-----
> From: Axel Urbiz <[hidden email]>
> Sent: Friday, May 15, 2020 5:33 PM
> To: Fox, John <[hidden email]>; [hidden email]
> Subject: "effects" package with "lme4"
>
> Hello John and others,
>
> I’d appreciate your help as I’m trying to plot the effect of predictor
> “Days” on Reaction by Subject. I’m only getting one plot in the example
> below.
>
> ### Start example
>
> library(lme4)
> library(splines)
> data("sleepstudy")
>
> fm1 <- lmer(Reaction ~ ns(Days, 3) + (ns(Days, 3) | Subject), sleepstudy)
> coef(fm1)
> plot(allEffects(fm1))
>
> ### End example
>
> Thanks,
> Axel.
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: "effects" package with "lme4"

Axel Urbiz
Dear John,

Thank you for your response.

My apologies as I’m only recently getting exposed to mixed-model. My understanding, is that the model specified below also has random intercepts and slope, as they vary by Subject. `coef(fm1)` shows this. I was looking to plot the fitted splines by Subject.

Sorry if my interpretation is incorrect.

Best,
Axel.



> On May 15, 2020, at 5:51 PM, Fox, John <[hidden email]> wrote:
>
> Dear Axel,
>
> There only one fixed effect in the model, ns(Days, 3), so I don't know what you expected.
>
> Best,
> John
> --------------------------------------
> John Fox, Professor Emeritus
> McMaster University
> Hamilton, Ontario, Canada
> Web: socialsciences.mcmaster.ca/jfox/
>
>
>
>> -----Original Message-----
>> From: Axel Urbiz <[hidden email]>
>> Sent: Friday, May 15, 2020 5:33 PM
>> To: Fox, John <[hidden email]>; [hidden email]
>> Subject: "effects" package with "lme4"
>>
>> Hello John and others,
>>
>> I’d appreciate your help as I’m trying to plot the effect of predictor
>> “Days” on Reaction by Subject. I’m only getting one plot in the example
>> below.
>>
>> ### Start example
>>
>> library(lme4)
>> library(splines)
>> data("sleepstudy")
>>
>> fm1 <- lmer(Reaction ~ ns(Days, 3) + (ns(Days, 3) | Subject), sleepstudy)
>> coef(fm1)
>> plot(allEffects(fm1))
>>
>> ### End example
>>
>> Thanks,
>> Axel.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: "effects" package with "lme4"

Fox, John
Dear Axel,

> On May 15, 2020, at 7:39 PM, Axel Urbiz <[hidden email]> wrote:
>
> Dear John,
>
> Thank you for your response.
>
> My apologies as I’m only recently getting exposed to mixed-model. My understanding, is that the model specified below also has random intercepts and slope, as they vary by Subject. `coef(fm1)` shows this. I was looking to plot the fitted splines by Subject.
>
> Sorry if my interpretation is incorrect.

There's no need to apologize.

The functions in the effects package compute and graph fixed effects for mixed models. You could compute and graph the BLUP for the fitted spline for each subject (see below) but it's not what the effects package does.

I think that the following does what you want:

-------- snip ----------

subjects <- levels(sleepstudy$Subject)
fits <- matrix(0, length(subjects), 10)
rownames(fits) <- subjects
for (subject in subjects){
  fits[subject, ] <- predict(fm1, newdata=data.frame(Subject=subject, Days=0:9))
}
plot(c(0, 10), range(fits), type="n", xlab="Days", ylab="Reaction")
for (subject in subjects) lines(0:9, fits[subject, ])

-------- snip ----------

Best,
 John
 

>
> Best,
> Axel.
>
>
>
>> On May 15, 2020, at 5:51 PM, Fox, John <[hidden email]> wrote:
>>
>> Dear Axel,
>>
>> There only one fixed effect in the model, ns(Days, 3), so I don't know what you expected.
>>
>> Best,
>> John
>> --------------------------------------
>> John Fox, Professor Emeritus
>> McMaster University
>> Hamilton, Ontario, Canada
>> Web: socialsciences.mcmaster.ca/jfox/
>>
>>
>>
>>> -----Original Message-----
>>> From: Axel Urbiz <[hidden email]>
>>> Sent: Friday, May 15, 2020 5:33 PM
>>> To: Fox, John <[hidden email]>; [hidden email]
>>> Subject: "effects" package with "lme4"
>>>
>>> Hello John and others,
>>>
>>> I’d appreciate your help as I’m trying to plot the effect of predictor
>>> “Days” on Reaction by Subject. I’m only getting one plot in the example
>>> below.
>>>
>>> ### Start example
>>>
>>> library(lme4)
>>> library(splines)
>>> data("sleepstudy")
>>>
>>> fm1 <- lmer(Reaction ~ ns(Days, 3) + (ns(Days, 3) | Subject), sleepstudy)
>>> coef(fm1)
>>> plot(allEffects(fm1))
>>>
>>> ### End example
>>>
>>> Thanks,
>>> Axel.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.