Margin effects plot for multiple objects.

huangapple go评论68阅读模式
英文:

r marginal effects plot for multiple objects

问题

My question is how to plot marginal effects or adjusted predictions using ggeffects() or ggemmeans() when I run my model on multiple subsets of data using dlply which creates summary estimates as multiple objects.

第一个问题是如何在使用dlply对多个数据子集运行模型时,使用ggeffects()ggemmeans()来绘制边际效应或调整预测值。

英文:

My question is how to plot marginal effects or adjusted predictions using ggeffects() or ggemmeans() when I run my model on multiple subsets of data using dlply which creates summary estimates as multiple objects

First, I start with a toy dataset.

   library(lme4)
   data(Soybean)
   library(plyr)
   library(nlme)
   library(ggplot2)
   library(ggeffects)

head(Soybean)

       Plot   Variety Year Time weight  
1988.1 1988F1       F 1988   14  0.106 
1988.2 1988F1       F 1988   21  0.261 
1988.3 1988F1       F 1988   28  0.666  
1988.4 1988F1       F 1988   35  2.110  
1988.5 1988F1       F 1988   42  3.560  
1988.6 1988F1       F 1988   49  6.230  

Second, I fit model with Variety as random intercept on each subset of dataset by Year.

table(Soybean$Year)

1988 1989 1990 
 156  128  128 
 
table(Soybean$Variety)

  F   P 
204 208 

# Fit LME model for each subset(Soybean)
MxM1 <- dlply(Soybean,
                    "Year",
                    function(x)
                      lme(fixed = weight ~ Time,
                          random = ~ 1 |Variety ,
                           data=x, na.action=na.exclude))

These are the coefficients of Random intercept (Variety) from the model.

2 varieties, 3 years , 6 different intercepts,

  ldply(MxM1, coef)
  Year (Intercept)  Time
1 1988       -8.65 0.349
2 1988       -7.73 0.349
3 1989       -6.71 0.242
4 1989       -4.04 0.242
5 1990       -7.28 0.309
6 1990       -6.63 0.309

How do I plot the marginal effects or adjusted predictions using ggeffects or ggemeans for lme estimates from multiple datasets ?

Expecting a marginal effects or conditional effects plot like this.

Margin effects plot for multiple objects.

答案1

得分: 1

以下是代码部分的中文翻译:

这是你正在寻找的吗?以下是来自carDataChile数据的示例。

library(ggplot2)
library(lme4)
library(dplyr)
library(ggeffects)
data("Chile", package="carData")
Chile$votey = ifelse(Chile$vote == "Y", 1, 0)

按照你建议的方式使用dlply()运行模型。

mods <- plyr::dlply(Chile, "sex", function(x)glmer(votey ~ age + education + income + (1|region), 
                                           data=x,
                                           family=binomial))

使用purrr中的map()生成预测,然后将结果转换为原始数据框(你也可以使用lapply())。然后,将这些预测合并为一个数据框。

preds <- purrr::map(mods, function(x)ggpredict(x, c("age[all]", "region"), type="re")  %>% as.data.frame())
preds <- bind_rows(preds, .id="sex")

然后,制作绘图:

ggplot(preds, aes(x=x, y=predicted, ymin=conf.low, ymax=conf.high, color=group, fill=group)) + 
  #geom_ribbon(col="transparent", alpha=.2) + 
  geom_line() + 
  facet_grid(sex ~ .)

图片在以下链接中:

Margin effects plot for multiple objects.

创建于2023-03-20,使用 reprex v2.0.2

英文:

Is something like this what you're looking for? Here's an example with the Chile data from carData.

library(ggplot2)
library(lme4)
library(dplyr)
library(ggeffects)
data(&quot;Chile&quot;, package=&quot;carData&quot;)
Chile$votey = ifelse(Chile$vote == &quot;Y&quot;, 1, 0)

Run the models as you suggest using dlply().

mods &lt;- plyr::dlply(Chile, &quot;sex&quot;, function(x)glmer(votey ~ age + education + income + (1|region), 
                                           data=x,
                                           family=binomial))

Use map() from purrr to generate predictions using ggpredict() and then turn the results into raw data frames. (you could just as easily use lapply().) Then, you bind the predictions together into a single data frame.

preds &lt;- purrr::map(mods, function(x)ggpredict(x, c(&quot;age[all]&quot;, &quot;region&quot;), type=&quot;re&quot;)  %&gt;% as.data.frame())
preds &lt;- bind_rows(preds, .id=&quot;sex&quot;)

Then, make the plot:

ggplot(preds, aes(x=x, y=predicted, ymin=conf.low, ymax=conf.high, color=group, fill=group)) + 
  #geom_ribbon(col=&quot;transparent&quot;, alpha=.2) + 
  geom_line() + 
  facet_grid(sex ~ .)

Margin effects plot for multiple objects.<!-- -->

<sup>Created on 2023-03-20 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年3月21日 02:33:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定