Specifying random effect nested under an interaction of fixed effects











up vote
1
down vote

favorite












Probably an easy one.



I have data with fixed and random effects I'd like to fit a mixed effects model to:



set.seed(1)

df <- data.frame(group = c(rep("A",40),rep("B",40)),
treatment = rep(c(rep("T",20),rep("CT",20)),2),
class = c(rep("AT1",10),rep("ACT1",10),rep("AT2",10),rep("ACT2",10),rep("BT1",10),rep("BCT1",10),rep("BT2",10),rep("BCT2",10)),
value = rnorm(80),
stringsAsFactors = F)

df$group <- factor(df$group, levels = c("A","B"))
df$treatment <- factor(df$treatment, levels = c("CT","T"))


The fixed effects are group and treatment and the random effect is class, which to my understanding is nested within the group and treatment combinations.



The model I want to fit is:



value ~ group*treatment


Where the effect of interest if the group:treatment interaction.
Of course I want to account for class as a random effect, but I can't seem to find what the syntax for that is. I tried:
(1|group*treatment/class) and (1|group:treatment/class) but both give an error.



Defining a group:treatment column in df:



df <- df %>% dplyr::mutate(group_treatment = paste0(group,"_",treatment))


And fitting:



fit <- lmer(value ~ group*treatment + (1|group_treatment/class), data = df)


Does seem to work, but I'm wondering if that's the only way or whether there's a more explicit syntax for such cases of random effect nesting.



Any idea?










share|improve this question




















  • 1




    As far as I know, a fixed effect predictor can't / shouldn't be used at the same time as random intercept. However, if you think that the effect of group on your outcome varies depending on class, than you can specify group and/or treatment as random slopes: lmer(value ~ group*treatment + (1 + group*treatment | class), data = df)
    – Daniel
    Nov 23 at 14:26















up vote
1
down vote

favorite












Probably an easy one.



I have data with fixed and random effects I'd like to fit a mixed effects model to:



set.seed(1)

df <- data.frame(group = c(rep("A",40),rep("B",40)),
treatment = rep(c(rep("T",20),rep("CT",20)),2),
class = c(rep("AT1",10),rep("ACT1",10),rep("AT2",10),rep("ACT2",10),rep("BT1",10),rep("BCT1",10),rep("BT2",10),rep("BCT2",10)),
value = rnorm(80),
stringsAsFactors = F)

df$group <- factor(df$group, levels = c("A","B"))
df$treatment <- factor(df$treatment, levels = c("CT","T"))


The fixed effects are group and treatment and the random effect is class, which to my understanding is nested within the group and treatment combinations.



The model I want to fit is:



value ~ group*treatment


Where the effect of interest if the group:treatment interaction.
Of course I want to account for class as a random effect, but I can't seem to find what the syntax for that is. I tried:
(1|group*treatment/class) and (1|group:treatment/class) but both give an error.



Defining a group:treatment column in df:



df <- df %>% dplyr::mutate(group_treatment = paste0(group,"_",treatment))


And fitting:



fit <- lmer(value ~ group*treatment + (1|group_treatment/class), data = df)


Does seem to work, but I'm wondering if that's the only way or whether there's a more explicit syntax for such cases of random effect nesting.



Any idea?










share|improve this question




















  • 1




    As far as I know, a fixed effect predictor can't / shouldn't be used at the same time as random intercept. However, if you think that the effect of group on your outcome varies depending on class, than you can specify group and/or treatment as random slopes: lmer(value ~ group*treatment + (1 + group*treatment | class), data = df)
    – Daniel
    Nov 23 at 14:26













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Probably an easy one.



I have data with fixed and random effects I'd like to fit a mixed effects model to:



set.seed(1)

df <- data.frame(group = c(rep("A",40),rep("B",40)),
treatment = rep(c(rep("T",20),rep("CT",20)),2),
class = c(rep("AT1",10),rep("ACT1",10),rep("AT2",10),rep("ACT2",10),rep("BT1",10),rep("BCT1",10),rep("BT2",10),rep("BCT2",10)),
value = rnorm(80),
stringsAsFactors = F)

df$group <- factor(df$group, levels = c("A","B"))
df$treatment <- factor(df$treatment, levels = c("CT","T"))


The fixed effects are group and treatment and the random effect is class, which to my understanding is nested within the group and treatment combinations.



The model I want to fit is:



value ~ group*treatment


Where the effect of interest if the group:treatment interaction.
Of course I want to account for class as a random effect, but I can't seem to find what the syntax for that is. I tried:
(1|group*treatment/class) and (1|group:treatment/class) but both give an error.



Defining a group:treatment column in df:



df <- df %>% dplyr::mutate(group_treatment = paste0(group,"_",treatment))


And fitting:



fit <- lmer(value ~ group*treatment + (1|group_treatment/class), data = df)


Does seem to work, but I'm wondering if that's the only way or whether there's a more explicit syntax for such cases of random effect nesting.



Any idea?










share|improve this question















Probably an easy one.



I have data with fixed and random effects I'd like to fit a mixed effects model to:



set.seed(1)

df <- data.frame(group = c(rep("A",40),rep("B",40)),
treatment = rep(c(rep("T",20),rep("CT",20)),2),
class = c(rep("AT1",10),rep("ACT1",10),rep("AT2",10),rep("ACT2",10),rep("BT1",10),rep("BCT1",10),rep("BT2",10),rep("BCT2",10)),
value = rnorm(80),
stringsAsFactors = F)

df$group <- factor(df$group, levels = c("A","B"))
df$treatment <- factor(df$treatment, levels = c("CT","T"))


The fixed effects are group and treatment and the random effect is class, which to my understanding is nested within the group and treatment combinations.



The model I want to fit is:



value ~ group*treatment


Where the effect of interest if the group:treatment interaction.
Of course I want to account for class as a random effect, but I can't seem to find what the syntax for that is. I tried:
(1|group*treatment/class) and (1|group:treatment/class) but both give an error.



Defining a group:treatment column in df:



df <- df %>% dplyr::mutate(group_treatment = paste0(group,"_",treatment))


And fitting:



fit <- lmer(value ~ group*treatment + (1|group_treatment/class), data = df)


Does seem to work, but I'm wondering if that's the only way or whether there's a more explicit syntax for such cases of random effect nesting.



Any idea?







nested lme4 mixed-models random-effects






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 9:09

























asked Nov 11 at 8:48









dan

1,38141042




1,38141042








  • 1




    As far as I know, a fixed effect predictor can't / shouldn't be used at the same time as random intercept. However, if you think that the effect of group on your outcome varies depending on class, than you can specify group and/or treatment as random slopes: lmer(value ~ group*treatment + (1 + group*treatment | class), data = df)
    – Daniel
    Nov 23 at 14:26














  • 1




    As far as I know, a fixed effect predictor can't / shouldn't be used at the same time as random intercept. However, if you think that the effect of group on your outcome varies depending on class, than you can specify group and/or treatment as random slopes: lmer(value ~ group*treatment + (1 + group*treatment | class), data = df)
    – Daniel
    Nov 23 at 14:26








1




1




As far as I know, a fixed effect predictor can't / shouldn't be used at the same time as random intercept. However, if you think that the effect of group on your outcome varies depending on class, than you can specify group and/or treatment as random slopes: lmer(value ~ group*treatment + (1 + group*treatment | class), data = df)
– Daniel
Nov 23 at 14:26




As far as I know, a fixed effect predictor can't / shouldn't be used at the same time as random intercept. However, if you think that the effect of group on your outcome varies depending on class, than you can specify group and/or treatment as random slopes: lmer(value ~ group*treatment + (1 + group*treatment | class), data = df)
– Daniel
Nov 23 at 14:26

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247140%2fspecifying-random-effect-nested-under-an-interaction-of-fixed-effects%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247140%2fspecifying-random-effect-nested-under-an-interaction-of-fixed-effects%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine