向修改后的outreg函数添加稳健标准错误的问题
我被困了好几个小时。我想向pauljohnsson(这里的原始代码)的R outreg函数添加健壮的标准错误(以及一些其他内容) 我有一个很大的问题,那就是如何使标准误差稳健。在我已替换的代码中向修改后的outreg函数添加稳健标准错误的问题,r,sweave,R,Sweave,我被困了好几个小时。我想向pauljohnsson(这里的原始代码)的R outreg函数添加健壮的标准错误(以及一些其他内容) 我有一个很大的问题,那就是如何使标准误差稳健。在我已替换的代码中 se<-sqrt(diag(vcov(model)))[regname] se我猜swave只是返回到包中的原始函数 我猜错了。见加文的回答。我认为这个问题可能是错误的根源,所以我把这个答案留作参考。但你的问题在别处 原始答复: 如果您选择了,您应该显式地加载所采用的函数。更重要的是,我会给你
se<-sqrt(diag(vcov(model)))[regname]
se我猜swave只是返回到包中的原始函数
我猜错了。见加文的回答。我认为这个问题可能是错误的根源,所以我把这个答案留作参考。但你的问题在别处
原始答复:
如果您选择了,您应该显式地加载所采用的函数。更重要的是,我会给你采用的函数起一个不同的名字(比如outreg2之类的),并使用它。如果不显式加载它,很可能会出现一个错误,说明找不到outreg2
另请注意:如果要在R会话中临时编辑函数,可以使用中讨论的选项之一。您在该代码中只编辑了对vcov()
调用的一个实例。您错过了本节:
if (tight == T) {
for (model in modelList) {
est <- coef(model)[regname]
if (!is.na(est)) cat (paste(" & (",round(sqrt(diag(vcov(model))[regname],3)),")",sep="")
else cat(" & ")
}
cat (" \\\\\n ")
}
if(紧密==T){
用于(模型列表中的模型){
est sandwich中没有outreg
函数,据我所知,他所做的只是使用该软件包中的函数,如果找不到outreg
,代码将失败,因此正在找到并使用该函数的某个版本。似乎不是这样。我已将该函数与source()一起粘贴到我的文件中。该函数可以工作,但在我更改前面提到的代码时,se不会变得健壮。该函数不是来自任何包。它是从文本文件中使用的。@Joris Meys。如何使函数显式?@Marcus:您确定加载了正确的函数吗?将其重命名为outreg2()
并在swave.R中调用该函数,如果它找不到新函数,应该会出错。目前,我怀疑您正在加载原始的outreg()
…您是否绝对确定正在加载(寻源)修改并保存的outreg()
在你的swave文件中?我认为@Joris的答案很接近,但看起来更像是你得到了Johnson的原始代码,而不是修改后的代码,并且没有实现。试着在Sewave文件中调用它outreg2()
,并将你的函数重命名为outreg2()
确保Swave找到了正确的函数。是的,我确实找到了它的源代码。我现在已将名称更改为outreg2()但是我产生了相同的输出=/我对代码做了其他更改,它们看起来很可能使用了我修改过的函数。好的,我想我现在有了它。检查我下面的答案。@Gavin:很好,错过了那个。@Gavin Simpson-我爱你!=)我将在这个函数上做更多的工作,并最终以某种方式将其公开。我有我们ed R只有几个月的时间,所以我必须先学习更多的som。@马库斯:放松点,我的妻子可能在看;-)@马库斯:你可以通过向上投票加文的答案来表达你的爱意,并接受它作为正确的答案(见他答案左边的箭头和V号)。我猜他的妻子可以接受;-)我已经将它标记为正确的现在=)不知道该函数。我不能作为未注册用户投票。过几天就可以了。现在非常忙!
### Paul Johnson
### Adapted from ideas in post in r-help by Dave Armstrong May 8, 2006
###tight means one column per fitted model
###not tight means 2 columns per fitted model
###incoming= either one regression model or a list of regresion models
###title = a string
###modelLabels= a VECTOR of character strings
### varLabels= a LIST of labels linked to variable names (see examples)
### tight= BOOLEAN, indicates results should be on one tight column or two for each model
### showAIC= BOOLEAN should the AIC be displayed for each model?
### lyx=create a table suitable for inclusion in a lyx float.
outreg <- function(incoming, title="My Regression", label="", modelLabels=NULL, varLabels=NULL, tight=TRUE, showAIC=TRUE, lyx=TRUE){
modelList <- NULL
## was input just one model, or a list of models? ###
if ( "lm" %in% class(incoming)) { ##just one model input
nmodels <- 1
modelList <- list(modl1=incoming)
} else {
nmodels <- length(incoming)
modelList <- incoming
}
##TODO modelLabels MUST have same number of items as "incoming"
## Get a regression summary object for each fitted model
summaryList <- list()
fixnames <- vector()
myModelClass <- vector()
i <- 1
for (model in modelList){
summaryList[[i]] <- summary(model)
fixnames <- unique( c( fixnames, names(coef(model))))
myModelClass[i] <- class(model)[1]
i <- i+1
}
###If you are just using LaTeX, you need these
if (lyx == FALSE){
cat("\\begin{table}\n ")
cat("\\caption{",title,"}\\label{",label,"}\n ")
}
cat("\\begin{center}\n ")
nColumns <- ifelse(tight, 1+nmodels, 1 + 2*nmodels)
cat(paste("\\begin{tabular}{*{",nColumns,"}{l}}\n ", sep=""))
cat("\\hline\n ")
### Put model labels on top of each model column, if modelLabels were given
if (!is.null(modelLabels)){
cat(" ")
for (modelLabel in modelLabels){
if (tight == T) {
cat(paste("&", modelLabel))
}else{
cat(paste("&\\multicolumn{2}{c}{",modelLabel,"}",sep=""))
}
}
cat (" \\\\\n ")
}
### Print the headers "Estimate" and "(S.E.)", output depends on tight or other format
if (tight == T){
cat(" ")
for (i in 1:nmodels) { cat (" & Estimate ") }
cat(" \\\\\n")
cat(" ")
for (i in 1:nmodels) { cat (" & (S.E.) ") }
cat(" \\\\\n")
}else{
cat(" ")
for (i in 1:nmodels) { cat (" & Estimate & S.E.") }
cat(" \\\\\n")
}
cat("\\hline \n \\hline\n ")
### Here come the regression coefficients
for (regname in fixnames){
if ( !is.null(varLabels[[regname]]) ) { cat(paste("",varLabels[[regname]]), sep="")}
else {cat(paste("", regname), sep="")}
for (model in modelList) {
est <- coef(model)[regname]
se <- sqrt(diag(vcov(model)))[regname]
if ( !is.na(est) ) {
cat (paste(" & ", round(est,3)))
pval <- pt(abs(est/se), lower.tail=F, df = model$df.residual)
if (pval < 0.025) cat("*")
if (tight == F) {
cat (paste(" & (", round(se,3),")",sep=""))
}
} else {
cat (" & . ")
if (tight == F) cat (" & " )
}
}
cat (" \\\\\n ")
if (tight == T){
for (model in modelList) {
est <- coef(model)[regname]
if (!is.na(est)) cat (paste(" & (",round(sqrt(diag(vcov(model)))[regname],3)),")",sep="")
else cat(" & ")
}
cat (" \\\\\n ")
}
}
cat("\\hline \n")
### Print a row for the number of cases
cat(paste("N"), sep="")
for (model in summaryList) {
myDF <- sum( model$df[-3] ) #omit third value from df vector
cat (paste(" & ", myDF))
if (tight == F) cat(" &")
}
cat (" \\\\\n ")
### Print a row for the root mean square error
if ("lm" %in% myModelClass) {
cat(paste("$RMSE$"),sep="")
for (model in summaryList) {
cat( paste(" &", if(is.numeric(model$sigma)) round(model$sigma,3)))
if (tight == F) cat(" &")
}
cat (" \\\\\n ")
}
### Print a row for the R-square
if ("lm" %in% myModelClass) {
cat(paste("$R^2$"),sep="")
for (model in summaryList) {
cat( paste(" &", if(is.numeric(model$r.square))round(model$r.square,3)))
if (tight == F) cat(" &")
}
cat (" \\\\\n ")
}
## Print a row for the model residual deviance
if ("glm" %in% myModelClass) {
cat(paste("$Deviance$"),sep="")
for (model in summaryList) {
cat (paste(" &", if(is.numeric(model$deviance))round(model$deviance,3)))
if (tight == F) cat(" &")
}
cat (" \\\\\n ")
}
### Print a row for the model's fit, as -2LLR
if ("glm" %in% myModelClass) {
cat (paste("$-2LLR (Model \\chi^2)$"),sep="")
for (model in modelList) {
if (is.numeric(model$deviance)){
n2llr <- model$null.deviance - model$deviance
cat (paste(" &", round(n2llr,3)))
gmdf <- model$df.null - model$df.residual + 1
if (pchisq(n2llr, df= gmdf, lower.tail=F) < 0.05) {cat ("*")}
}
else {
cat (" &")
}
if (tight == F) cat(" &")
}
cat (" \\\\\n ")
}
## Print a row for the model's fit, as -2 LLR
### Can't remember why I was multiplying by -2
if (showAIC == T) {
cat(paste("$AIC$"),sep="")
for (model in modelList) {
cat (paste(" &", if(is.numeric(AIC(model)))round(AIC(model),3)))
if (tight == F) cat(" &")
}
cat (" \\\\\n ")
}
cat("\\hline\\hline\n")
cat ("* $p \\le 0.05$")
cat("\\end{tabular}\n")
cat("\\end{center}\n")
if (lyx == FALSE){
cat("\\end{table}\n")
}
}
if (tight == T) {
for (model in modelList) {
est <- coef(model)[regname]
if (!is.na(est)) cat (paste(" & (",round(sqrt(diag(vcov(model))[regname],3)),")",sep="")
else cat(" & ")
}
cat (" \\\\\n ")
}