从R中的回归标准误差再现均值差标准误差

时间:2020-07-02 15:52:12

标签: r regression

我正在尝试从回归分析中提取均值差标准误。目前,我正在使用此代码

Posdef <- function (n, ev = runif(n, 0, 10)) 
    {
    Z <- matrix(ncol=n, rnorm(n^2))
        decomp <- qr(Z)
        Q <- qr.Q(decomp) 
        R <- qr.R(decomp)
        d <- diag(R)
        ph <- d / abs(d)
        O <- Q %*% diag(ph)
        Z <- t(O) %*% diag(ev) %*% O
        return(Z)
    }
Sigma <- Posdef(n = 12)
mu <-  runif(12,0,10) 

#setting the random data to be perfectly aligned with the population using
#'empirical = TRUE'

data <- as.data.frame(mvrnorm(n=100000, mu, Sigma, empirical = TRUE))
names(data) = c('criteria_1', 'criteria_2', 'criteria_3', 'criteria_4', 'criteria_5', 
            'criteria_6', 'criteria_7', 'criteria_8', 'criteria_9', 'criteria_10',
            'outcome', 'effect')
data$effect <- ifelse(data$effect > mean(data$effect), 1, 0)


sample <- data[sample(nrow(data), floor(runif(1, min=25, max=200))), ]

data1 <- subset (sample, effect > 0)
mean1 <- mean (data1$outcome)
data2 <- subset (sample, effect < 1)
mean2 <- mean (data2$outcome)
pooledsd <- sqrt (((sd(data2$outcome)^2)+(sd(data1$outcome)^2))/2)
ES <- (mean1 - mean2)/pooledsd
SE <- sqrt(pooledsd*(1/(nrow(data1)) + 1/(nrow(data2))))

model <- lm (formula = outcome ~ effect, data = sample)

modelspec <- model$coef %>%
        as.data.frame ()
d1 <- setDT(modelspec, keep.rownames = TRUE)[]
colnames (d1) [2] <- "beta"
d2 <- subset (d1, d1$rn == "effect")

sample_cor <- (sd(sample$effect)/sd(sample$outcome))* d2$beta
sample_d <- (2*sample_cor)/sqrt(1-((sample_cor)^2))

se_out <- as.data.frame (sqrt(diag(vcov(model))))
sample_se <- se_out [1,]

我的sample_se绝对不等于我的SE,因为它只是截距的标准错误。

有没有办法得到等价物

sqrt(pooledsd*(1/(nrow(data1)) + 1/(nrow(data2))))

来自

model <- lm (formula = outcome ~ effect, data = sample)

0 个答案:

没有答案