簡體   English   中英

如何 R 中的 plot sigmoidal 數據 - 二進制 Y 連續 X ggplot 混合效應邏輯回歸

[英]How to plot sigmoidal data in R - binary Y continuous X ggplot mixed effects logisitic regression

這是我正在使用的數據:

data <- data.frame(id = rep(1:3, each = 30), 
           intervention = rep(c("a","b"),each= 2, times=45),
           area = rep(1:3, times=30), 
           "dv1" = rnorm(90, mean =10, sd=7),
           "dv2" = rnorm(90, mean =5, sd=3),
           outcome = rbinom(90, 1, prob=.5))

data$id <- as.factor(data$id)
data$intervention <- as.factor(data$intervention)
data$area <- as.factor(data$area)
data$outcome <- as.factor(data$outcome)

我正在嘗試為這種混合效應邏輯回歸 model 制作 sigmoidal 圖:

library(lmer4)
glmer(
  outcome1 ~ dv1 + (1 | id/area), 
  data = data, 
  family = binomial(link = "logit")
)

這是我嘗試過但失敗的方法:

library(ggplot2)
ggplot(data, aes(x=dv1, y=outcome1, color=factor(area))) + 
  facet_wrap(~id) +
  geom_point() + 
  stat_smooth(method="glm", method.args=list(family="binomial"), color="black", se=F)

Info    
`geom_smooth()` using formula 'y ~ x'
Warning 
Computation failed in `stat_smooth()`: y values must be 0 <= y <= 1
Computation failed in `stat_smooth()`: y values must be 0 <= y <= 1
Computation failed in `stat_smooth()`: y values must be 0 <= y <= 1

在此處輸入圖像描述

此外,這甚至是 plot 邏輯回歸的正確方法嗎? 我應該從 model 本身提取一些數據還是出於說明性原因繪制原始數據就足夠了?

對我來說(不是專家)看起來不錯 - 我認為問題在於您的樣本數據不是特別“邏輯”(即 dv1 的傳播與結果在邏輯上不相關)。 如果您修改示例數據,例如

library(tidyverse)
#install.packages("lme4")
library(lme4)
set.seed(123)
data <- data.frame(id = rep(1:3, each = 30), 
                   intervention = rep(c("a","b"), each= 2, times=45),
                   area = rep(1:3, times = 30), 
                   "dv1" = rep(c(rnorm(15, mean = 20, sd = 7),
                                 rnorm(15, mean = 40, sd = 7)), times = 3),
                   "dv2" = rep(c(rnorm(15, mean = 20, sd = 7),
                                 rnorm(15, mean = 40, sd = 7)), times = 3),
                   outcome = rep(c(rbinom(15, 0, prob = .95),
                                   rbinom(15, 1, prob = .95)), times = 3))

data$id <- as.factor(data$id)
data$intervention <- as.factor(data$intervention)
data$area <- as.factor(data$area)
data$outcome <- as.factor(data$outcome)

model_1 <- glmer(
  outcome ~ dv1 + (1 | id/area), 
  data = data, 
  family = binomial(link = "logit")
)

library(ggplot2)
ggplot(data, aes(x = dv1, y = as.numeric(outcome) - 1, color = factor(area))) +
  stat_smooth(method="glm", color="black", se=FALSE,
              method.args = list(family=binomial)) + 
  geom_point() +
  facet_wrap(~id)

plot 看起來更像您所期望的:

示例_1.png

(注意:這三個面板是相同的,因為我重復了樣本數據 3 次,但你明白了)

If you want to plot the model predictions, this tutorial gives a straightforward overview: https://mgimond.github.io/Stats-in-R/Logistic.html

暫無
暫無

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

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