【问题标题】:Simulate data for logistic regression with fixed r2使用固定 r2 模拟逻辑回归数据
【发布时间】:2018-08-24 07:34:00
【问题描述】:

我想模拟逻辑回归的数据,我可以预先指定其解释方差。看看下面的代码。我模拟了四个自变量,并指定每个 logit 系数的大小应为 log(2)=0.69。这很好用,解释的方差(我报告 Cox & Snell 的 r2)为 0.34。

但是,我需要指定回归系数,以使预先指定的 r2 将从回归中产生。因此,如果我想产生一个 r2,假设正好是 0.1。如何指定系数?我有点挣扎这个..

# Create independent variables
sigma.1 <- matrix(c(1,0.25,0.25,0.25,   
                0.25,1,0.25,0.25,   
                0.25,0.25,1,0.25,    
                0.25,0.25,0.25,1),nrow=4,ncol=4)
mu.1 <- rep(0,4) 
n.obs <- 500000 

library(MASS)
sample1 <- as.data.frame(mvrnorm(n = n.obs, mu.1, sigma.1, empirical=FALSE))

# Create latent continuous response variable 
sample1$ystar <- 0 + log(2)*sample1$V1 + log(2)*sample1$V2 + log(2)*sample1$V3 + log(2)*sample1$V4

# Construct binary response variable
sample1$prob <- exp(sample1$ystar) / (1 + exp(sample1$ystar))
sample1$y <- rbinom(n.obs,size=1,prob=sample1$prob)

# Logistic regression
logreg <- glm(y ~ V1 + V2 + V3 + V4, data=sample1, family=binomial)
summary(logreg)

输出是:

Call:
glm(formula = y ~ V1 + V2 + V3 + V4, family = binomial, data = sample1)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-3.7536  -0.7795  -0.0755   0.7813   3.3382  

Coefficients:
             Estimate Std. Error z value Pr(>|z|)    
(Intercept) -0.002098   0.003544  -0.592    0.554    
V1           0.691034   0.004089 169.014   <2e-16 ***
V2           0.694052   0.004088 169.776   <2e-16 ***
V3           0.693222   0.004079 169.940   <2e-16 ***
V4           0.699091   0.004081 171.310   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 693146  on 499999  degrees of freedom
Residual deviance: 482506  on 499995  degrees of freedom
AIC: 482516

Number of Fisher Scoring iterations: 5

Cox 和 Snell 的 r2 给出:

library(pscl)
pR2(logreg)["r2ML"]

> pR2(logreg)["r2ML"]
 r2ML 
0.3436523 

【问题讨论】:

  • 为什么说independent变量?
  • 对象 sample1 由四个 x 变量组成,这些变量在回归中用作 y 的预测变量。它们是使用 mvrnorm 函数从总体平均向量 mu.1 和协方差矩阵 sigma.1 中得出的。这有帮助吗?
  • 我认为这个问题被严重低估了。我假设您找不到一个令人满意的答案,即仅暗示 Y 与 X1 的相关性与 X2、X3、X4 与 Y 和所有其他预测变量的零相关性。

标签: r logistic-regression variance


【解决方案1】:

如果您在 ystar 变量中添加一个随机误差项来制作 ystat.r 然后使用它,您可以调整标准偏差,直到它符合您的规范。

sample1$ystar.r <- sample1$ystar+rnorm(n.obs, 0, 3.8)  # tried a few values
sample1$prob <- exp(sample1$ystar.r) / (1 + exp(sample1$ystar.r))
sample1$y <- rbinom(n.obs,size=1,prob=sample1$prob)
logreg <- glm(y ~ V1 + V2 + V3 + V4, data=sample1, family=binomial)
summary(logreg)  # the estimates "shrink"
pR2(logreg)["r2ML"]
#-------
     r2ML 
0.1014792

【讨论】:

    【解决方案2】:

    R 平方(及其变体)是一个随机变量,因为它取决于您的模拟数据。如果您多次使用完全相同的参数模拟数据,您很可能每次都会得到不同的 R 平方值。因此,您无法仅通过控制参数来生成 R 平方正好为 0.1 的模拟。

    另一方面,由于它是一个随机变量,您可能会根据条件分布模拟您的数据(以 R 平方的固定值为条件),但您需要找出这些分布是什么样的(数学在这里可能会变得很丑,cross validated 更适合这部分)。

    【讨论】:

    • 您好 Freguglia,感谢您的回答!您评论的后半部分正是我的目标。当然,与每个参数一样,R2 也会受到采样变化的影响,但在给定系数的情况下,R2 肯定存在预期或“真实值”。由于逻辑回归中的 R2 是完整模型和空模型可能性的函数,因此我基本上需要确定系数与我认为的这些可能性值之间的关系......
    猜你喜欢
    • 2022-10-04
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 2015-06-30
    • 2020-12-09
    • 2019-02-15
    相关资源
    最近更新 更多