我需要一个“nls”对象的LaTex表示。不幸的是,stargazer不支持这种对象类型。
对网络的一些研究使我得到了as.lm.nls函数的nls2库。它声称将nls对象转换为相应的lm对象。在我的情况下,它失败了。以下是示例输出:
> DNase1 <- subset(DNase, Run == 1)
> xx <- nls(density ~ SSlogis(log(conc), Asym, xmid, scal), DNase1)
> summary(xx)
Formula: density ~ SSlogis(log(conc), Asym, xmid, scal)
Parameters:
Estimate Std. Error t value Pr(>|t|)
Asym 2.34518 0.07815 30.01 2.17e-13 ***
xmid 1.48309 0.08135 18.23 1.22e-10 ***
scal 1.04146 0.03227 32.27 8.51e-14 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.01919 on 13 degrees of freedom
Number of iterations to convergence: 0
Achieved convergence tolerance: 3.281e-06
下面的as.lm.nls输出与上面的实际输出不匹配:
> library(nls2)
Loading required package: proto
> xlm = as.lm.nls(xx)
> summary(xlm)
Call:
lm(formula = density ~ Asym + xmid + scal - 1, offset = fitted(xx))
Residuals:
Min 1Q Median 3Q Max
-0.033513 -0.012931 -0.001454 0.009699 0.038137
Coefficients:
Estimate Std. Error t value Pr(>|t|)
Asym -8.878e-07 7.815e-02 0 1
xmid -9.328e-07 8.135e-02 0 1
scal -3.751e-07 3.227e-02 0 1
Residual standard error: 0.01919 on 13 degrees of freedom
Multiple R-squared: 0.9996, Adjusted R-squared: 0.9995
F-statistic: 1.153e+04 on 3 and 13 DF, p-value: < 2.2e-16
由于nls摘要输出与lm摘要输出非常相似,我认为nls到lm对象的内容等价物应该可以让stargazer发挥其魅力。
我有很多nls模型要包含在报告中,as.lm.nls的失败让我陷入困境。我需要绝望的帮助乳胶输出nls对象。
关于如何继续的任何指针。
问候
ķ
答案 0 :(得分:2)
仔细阅读stargazer的参数签名后,可以使用coef,se,t和p参数生成tex格式的nls模型输出。你需要一个lm模型开始,并用这些参数替换必要的部分。以下是快速入侵,可以对其进行适当的修改。
library(stargazer)
### start with an arbitrary lm model, following suited for the given situation
lm1 = lm(rating ~ complaints + privileges + learning - 1, data=attitude)
fakeX = c("complaints", "privileges", "learning")
### nls model to be represented by stargazer
DNase1 <- subset(DNase, Run == 1)
xx <- nls(density ~ SSlogis(log(conc), Asym, xmid, scal), DNase1)
summary(xx)
### generate various parts for output
sum_xx = summary(xx)
mat_xx = sum_xx$coefficients
colnames(mat_xx) = c("coef","se", "t", "p")
indVarNames = rownames(mat_xx)
### generate coefficients, se, t-stat and p values
df_xx = as.data.frame(mat_xx)
vCoef = df_xx$coef; names(vCoef)=fakeX
vSE = df_xx$se; names(vSE)=fakeX
vT = df_xx$t; names(vT)=fakeX
vP = df_xx$p; names(vP)=fakeX
formulaTxt = sum_xx$formula
nParameters = sum_xx$df[1]
nDF = sum_xx$df[2]
rss = round(sum_xx$sigma, 3)
convTolerance = xx$m$conv()
### various aesthetics for stargazer
vTitle = "Regression Results Model: "
vType = "latex"
vDepLabel = c("density")
outFile=c("./model.tex")
vLines=c(sprintf("RSE: %0.3f", rss), sprintf("df = %d", nDF))
vNotes=c(sprintf("Achieved convergence tolerance: %0.5f", convTolerance))
### and the output follows
outStar = stargazer(lm1, title = vTitle, out=outFile, out.header=T,
no.space=T, digits=3, type=vType, single.row=T,
omit.stat = c("rsq","adj.rsq", "f", "n", "ser"),
covariate.labels = indVarNames, dep.var.labels = vDepLabel,
add.lines=list(vLines),
notes=vNotes, notes.append=T,
coef=list(vCoef), se=list(vSE), t=list(vT), p=list(vP)
)
答案 1 :(得分:1)
对于LaTeX中的自定义输出表,我的经验是xtable
包是最方便,最灵活的解决方案。
让我们考虑一下你有nls
对象的位置,而观星者不支持它(可能有一天会这样?)。
### Estimate model
model <- nls(Y ~ <your function>(theta1, theta2, theta3))
### generate various parts for output
sum_model = summary(model)
mat_model = sum_model$coefficients
### generate coefficients, se, t-stat and p values
df_model = as.data.frame(mat_model)
colnames(df_model) <- c("Coef.", "Std. error", "t-stat.", "p")
mat <- data.frame(t(df_model))
tbl <- xtable(mat)
print(tbl, only.contents=TRUE, include.rownames=T,
include.colnames=F, floating=F,
hline.after=NULL,
file="summary_nls.tex")
现在请注意,您有很多可能使用R包xtable
来定制输出以满足您的需求。例如,我省略了整个LaTeX tabular
环境,我在stargazer
中永远无法摆脱它,我喜欢将threeparttable
包与LaTeX包中的命令一起使用{{1 }}。对于给定的玩具模型,整洁的输出将如下所示:
booktabs
我将上面的代码保留为模板,只需为更大,更复杂的模型添加行和列。与\begin{table}[t]
\centering
\begin{threeparttable}
\captionabove{Regression results for nonlinear model.}
\begin{tabular}{lccc}
\toprule
& Theta1 & Theta2 & Theta3\\
& $\theta 1$ & $\beta$ & $\sigma$ \\
\midrule
\input{summary_nls}
\bottomrule
\end{tabular}
\label{tab:nls_summary}
\end{threeparttable}
\end{table}
配对,这对我来说就像是一种魅力。