簡體   English   中英

order.terms 不會重新排序 sjPlot 的 plot_model 中的術語

[英]order.terms does not reorder terms in sjPlot's plot_model

我有代碼來繪制一個簡單的三級單因子回歸圖,但我無法說服 sjPlot 對 X 軸上的項重新排序,我想知道是否有人可以幫助我弄清楚發生了什么。

我的代碼:

m0 <- lmer(ans ~ type + (1|subject/target), data=behavioral_data)
summary(m0)

p1 <- plot_model(m0, 
                 type = "pred", 
                 terms = c("type"),
                 order.terms = c(2, 1),
                 auto.label = F,
                 title = "Model Estimates of Answer (Marginal Effects)",
                 axis.title = c("Target Type", "Answer")
)

output model 摘要產生:

Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest']
Formula: ans ~ type + (1 | subject/target)
   Data: behavioral_data

REML criterion at convergence: 15354

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.8944 -0.7136 -0.1561  0.6646  3.2381 

Random effects:
 Groups         Name        Variance Std.Dev.
 target:subject (Intercept) 0.1434   0.3787  
 subject        (Intercept) 0.3051   0.5524  
 Residual                   1.7003   1.3040  
Number of obs: 4447, groups:  target:subject, 444; subject, 37

Fixed effects:
             Estimate Std. Error        df t value Pr(>|t|)    
(Intercept)   2.74088    0.10181  48.14515  26.922   <2e-16 ***
typeN        -0.03277    0.06509 404.96582  -0.503   0.6149    
typeY        -0.14263    0.06506 404.00056  -2.193   0.0289 *  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

我得到的圖表:

在此處輸入圖像描述

我希望order.terms = c(2, 1)重新排序 Y 和 N。我錯過了什么?

由於 plot_model() 返回的是一個 ggplot 對象,您可以添加其他與 ggplot 相關的函數來后期修改 plot。 例如,試試這個:

p1 <- plot_model(m0, 
                 type = "pred", 
                 terms = c("type"),
                 order.terms = c(2, 1),
                 auto.label = F,
                 title = "Model Estimates of     Answer (Marginal Effects)",
                 axis.title = c("Target Type", "Answer")
) + scale_x_discrete(limits=c("S", "Y", "N"))

離散系列中的 limits=() 參數可以接受一個字符向量來指示 x 軸的順序。

或者,您可以應用 forcats package 中的 fct_relevel() function,它將以相同的方式對所有未來回歸的因素進行排序。 在運行回歸之前將此 function 應用於原始數據集,plot_model function 解決了此問題。

例子:

data2 <- data %>%
  mutate(name = fct_relevel(name, 
            "north", "north-east", "east", 
            "south-east", "south", "south-west", 
            "west", "north-west"))

現在,當您將 data.table 應用於回歸並使用 plot_model 將 plot 應用於回歸時,因子將按上述順序顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM