kishore
kishore

Reputation: 541

how to get beautiful latex output of "nls" object

I need LaTex representation of an "nls" object. Unfortunately stargazer doesn't support this object type.

Some research on the net led me to as.lm.nls function out of nls2 library. It claims to convert an nls object to corresponding lm object. In my case it failed miserably. Below is the sample output:

> 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

And as.lm.nls output below doesn't match the actual output above:

> 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

Since nls summary output is quite similar to lm summary output, I assume that content equivalent of nls to lm object should be OK for stargazer to work its charm.

I have quite a number of nls models to be incorporated in the report, and failure of as.lm.nls has put me in bind. I need desperate help with latex output of nls object.

Any pointer about how do I proceed.

regards

K

Upvotes: 3

Views: 2019

Answers (2)

pfifas
pfifas

Reputation: 592

For custom output tables in LaTeX my experience is that the xtablepackage is the most convenient, yet flexible solution.

Lets consider your case of where you have an nlsobject and stargazer does not support it (maybe it will some day?).

### 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")

Now note that you have many possibilities to tailor the output to your needs with the R package xtable. For example, I omit the entire LaTeX tabular environment, which I could never get rid of in stargazer and I like to use the threeparttable package together with commands from the LaTeX package booktabs. For the given toy model a neat output would look like this:

\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}

I keep the code above as a template and just add rows and columns for larger, more sophisticated models. Paired with knitr this works like a charm for me.

Upvotes: 3

kishore
kishore

Reputation: 541

After careful reading of the arguments signature for stargazer, nls model output in tex format can be generated using coef, se, t, and p arguments. You do need an lm model to begin with and replace the necessary parts with these arguments. Below is a quick hack and appropriate modifications can be made to it.

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)
)

Upvotes: 2

Related Questions