R logistic回归中不同因素的致死剂量(LD50)

R logistic回归中不同因素的致死剂量(LD50),r,logistic-regression,glm,R,Logistic Regression,Glm,我有一个逻辑回归模型,包含一个连续变量和一个具有不同水平/因素的定性变量。有没有办法计算每个因素的LD50 library(dplyr) library(ggeffects) library(MASS) # Produce a table with data set.seed(321) x1 <- rnorm(400) x2 <- 3*x1^3 + 2*x1^2 + x1 + 1 x3 <- 1/(1 + exp(-x2)) x4 = rbinom(400, 1, x3)

我有一个逻辑回归模型,包含一个连续变量和一个具有不同水平/因素的定性变量。有没有办法计算每个因素的LD50

library(dplyr)
library(ggeffects)
library(MASS)

# Produce a table with data
set.seed(321)

x1 <- rnorm(400)
x2 <- 3*x1^3 + 2*x1^2 + x1 + 1
x3 <- 1/(1 + exp(-x2))
x4 = rbinom(400, 1, x3) 

dt <- tibble(binary = x4,
             continuous = x1,
             qualitative = sample(c("white", "pink", "lime"), 400, TRUE))

# Logistic model
mdl <- glm(binary ~ continuous + qualitative, family = "binomial", data = dt)

# Calculate LD50, which gives the lethal dose for the whole model
dose.p(mdl) # -0.58 ± 0.11 se

# Plot regression model by factor of the qualitative variable
newdat <- ggpredict(mdl, terms = c("continuous[all]", "qualitative"))
plot(newdat)
库(dplyr)
图书馆(ggeffects)
图书馆(弥撒)
#生成包含数据的表
种子集(321)

x1以下内容适用于每个因素的二进制与连续模型,并得出LD50和SE:

get_LD50 = function(fit){
   data.frame(
   LD50 = dose.p(fit)[1],
   SE = attributes(dose.p(fit))$SE[,1]
   )
}

dt %>% group_by(qualitative) %>% 
do(get_LD50(glm(binary ~ continuous, family = "binomial", data = .)))

# A tibble: 3 x 3
# Groups:   qualitative [3]
  qualitative   LD50    SE
  <chr>        <dbl> <dbl>
1 lime        -0.578 0.115
2 pink        -0.479 0.104
3 white       -0.392 0.116
get_LD50=功能(fit){
数据帧(
LD50=剂量p(适合)[1],
SE=属性(剂量p(适合))$SE[,1]
)
}
dt%>%分组依据(定性)%>%
do(获取LD50(glm(二进制连续,family=“二项式”,data=))
#一个tibble:3x3
#分组:定性[3]
定性LD50 SE
1石灰-0.578 0.115
2粉红色-0.479 0.104
3白色-0.392 0.116

如果您在统计方面需要更复杂的东西,您必须澄清

谢谢您的同事。这看起来是个好办法。但是,我应该提到(对不起,是我的错!)连续变量在实际数据中只取正值。当我应用你的函数时,有两个LD50值是负值,这是不可解释的,从生态学角度讲,连续变量不能为负值。恐怕这与我的解决方案无关。我只使用质量中的dose.p函数,该函数根据拟合计算ld50。如果你得到的ld50没有意义,可能是因为不合适,或者你的数据需要清理。这两个问题都超出了这个问题的范围,或者我担心是这样。如果您查看提供的示例u,则计算的ld50是正确的。