将系数乘以标准偏差

时间:2015-03-05 18:18:30

标签: r stargazer

R中,stargazer包提供了将函数应用于系数,标准错误等的可能性:

dat <- read.dta("http://www.ats.ucla.edu/stat/stata/dae/nb_data.dta")
dat <- within(dat, {
    prog <- factor(prog, levels = 1:3, labels = c("General", "Academic", "Vocational"))
    id <- factor(id)
})
m1 <- glm.nb(daysabs ~ math + prog, data = dat)
transform_coef <- function(x) (exp(x) - 1)
stargazer(m1, apply.coef=transform_coef)

如何应用一个函数,其中我乘以的因子取决于变量,如该变量的标准偏差?

1 个答案:

答案 0 :(得分:0)

这可能与您的预期完全不同,但您可以转换系数,并为stargazer提供自定义list个系数。例如,如果您想报告系数乘以每个变量的标准偏差,则示例的以下扩展可以起作用:

library(foreign)
library(stargazer)
library(MASS)

dat <- read.dta("http://www.ats.ucla.edu/stat/stata/dae/nb_data.dta")
dat <- within(dat, {
  prog <- factor(prog, levels = 1:3, labels = c("General", "Academic", "Vocational"))
  id <- factor(id)
})
m1 <- glm.nb(daysabs ~ math + prog, data = dat)

# Store coefficients (and other coefficient stats)
s1 <- summary(m1)$coefficients

# Calculate standard deviations (using zero for the constant)
math.sd  <- sd(dat$math)
acad.sd  <- sd(as.numeric(dat$prog == "Academic"))
voc.sd   <- sd(as.numeric(dat$prog == "Vocational"))
int.sd   <- 0

# Append standard deviations to stored coefficients
StdDev   <- c(int.sd, math.sd, acad.sd, voc.sd)
s1       <- cbind(s1, StdDev)

# Store custom list
new.coef <- s1[ , "Estimate"] * s1[ , "StdDev"]

# Output
stargazer(m1, coef = list(new.coef))

您可能需要考虑原始问题之外的一些关于在stargazer中输出系数的问题。你应该在乘以标准偏差时报告截距吗?您的标准错误和推断是否与此转换相同?