2014-05-05 56 views
2

我需要一個「nls」對象的LaTex表示。不幸的是,stargazer不支持這種對象類型。如何獲得「nls」對象的美麗乳膠輸出

在網絡上的一些研究導致我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摘要輸出頗爲相似l米摘要輸出,我假定相當於nls到lm對象的內容應該可以用於觀星者發揮其魅力。

我有相當數量的納入模型納入報告,as.lm.nls的失敗已經把我綁定。我需要對乳膠輸出的nls對象絕望的幫助。

任何關於如何進行的指針。

問候

ķ

回答

2

後爲斯塔蓋澤參數簽名的仔細閱讀,可使用COEF,SE,t和p參數來產生在特格式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) 
) 
0

對於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包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} 

我把上面作爲模板的代碼,只需添加的行和列的更大,更復雜的模型。配對knitr這對我來說就像是一種魅力。