获得通过最大似然估计的系数到星形表中

时间:2014-01-24 17:17:55

标签: r optimization lm stargazer

Stargazer为lm(和其他)物体生产非常漂亮的乳胶桌。假设我按最大可能性拟合模型。我希望观星者为我的估计生成一个类似于lm的表格。我怎么能这样做?

虽然它有点hacky,但有一种方法可能是创建一个包含我估计值的“假”lm对象 - 我认为只要summary(my.fake.lm.object)有效,这就可以工作。这很容易吗?

一个例子:

library(stargazer)

N <- 200
df <- data.frame(x=runif(N, 0, 50))
df$y <- 10 + 2 * df$x + 4 * rt(N, 4)  # True params
plot(df$x, df$y)

model1 <- lm(y ~ x, data=df)
stargazer(model1, title="A Model")  # I'd like to produce a similar table for the model below

ll <- function(params) {
    ## Log likelihood for y ~ x + student's t errors
    params <- as.list(params)
    return(sum(dt((df$y - params$const - params$beta*df$x) / params$scale, df=params$degrees.freedom, log=TRUE) -
               log(params$scale)))
}

model2 <- optim(par=c(const=5, beta=1, scale=3, degrees.freedom=5), lower=c(-Inf, -Inf, 0.1, 0.1),
                fn=ll, method="L-BFGS-B", control=list(fnscale=-1), hessian=TRUE)
model2.coefs <- data.frame(coefficient=names(model2$par), value=as.numeric(model2$par),
                           se=as.numeric(sqrt(diag(solve(-model2$hessian)))))

stargazer(model2.coefs, title="Another Model", summary=FALSE)  # Works, but how can I mimic what stargazer does with lm objects?

更确切地说:对于lm个对象,stargazer很好地打印表格顶部的因变量,包括相应估计值下面的括号中的SE,并且在表格的底部有R ^ 2和观察数量。是否有一种(简单的)方法来获得与最大似然估计的“自定义”模型相同的行为,如上所述?

以下是我将我的优化输出打扮成lm对象的微弱尝试:

model2.lm <- list()  # Mimic an lm object
class(model2.lm) <- c(class(model2.lm), "lm")
model2.lm$rank <- model1$rank  # Problematic?
model2.lm$coefficients <- model2$par
names(model2.lm$coefficients)[1:2] <- names(model1$coefficients)
model2.lm$fitted.values <- model2$par["const"] + model2$par["beta"]*df$x
model2.lm$residuals <- df$y - model2.lm$fitted.values
model2.lm$model <- df
model2.lm$terms <- model1$terms  # Problematic?
summary(model2.lm)  # Not working

3 个答案:

答案 0 :(得分:2)

我只是遇到了这个问题并通过在观星者中使用coef seomit函数克服了这个问题......例如。

stargazer(regressions, ...
                     coef = list(... list of coefs...),
                     se = list(... list of standard errors...),
                     omit = c(sequence),
                     covariate.labels = c("new names"),
                     dep.var.labels.include = FALSE,
                     notes.append=FALSE), file="")

答案 1 :(得分:1)

您需要首先实例化一个虚拟lm对象,然后打扮它:

#...
model2.lm = lm(y ~ ., data.frame(y=runif(5), beta=runif(5), scale=runif(5), degrees.freedom=runif(5)))
model2.lm$coefficients <- model2$par
model2.lm$fitted.values <- model2$par["const"] + model2$par["beta"]*df$x
model2.lm$residuals <- df$y - model2.lm$fitted.values
stargazer(model2.lm, se = list(model2.coefs$se), summary=FALSE, type='text')

# ===============================================
#                         Dependent variable:    
#                     ---------------------------
#                                  y             
# -----------------------------------------------
# const                        10.127***         
#                               (0.680)          
#                                                
# beta                         1.995***          
#                               (0.024)          
#                                                
# scale                        3.836***          
#                               (0.393)          
#                                                
# degrees.freedom              3.682***          
#                               (1.187)          
#                                                
# -----------------------------------------------
# Observations                    200            
# R2                             0.965           
# Adjusted R2                    0.858           
# Residual Std. Error       75.581 (df = 1)      
# F Statistic              9.076 (df = 3; 1)     
# ===============================================
# Note:               *p<0.1; **p<0.05; ***p<0.01

(然后当然要确保剩余的摘要统计数据是正确的)

答案 2 :(得分:0)

我不知道你是如何使用stargazer,但你可以尝试使用扫帚和xtable包,问题是它不会给你优化模型的标准错误

library(broom)
library(xtable)
xtable(tidy(model1))
xtable(tidy(model2))