BBrill - 3 months ago 15

R Question

I'm making a table to compare different linear models using r and latex, through the package texreg (It is my first time with this package). I find this package very useful, but I really cannot understand why the RMSE is not included in the extract.lm method.

I have modified the extract.lm method on this way to include it as option:

`extract.lm<- function (model, include.rsquared = TRUE, include.adjrs = TRUE, include.rmse = TRUE,`

include.nobs = TRUE, ...)

{

s <- summary(model, ...)

names <- rownames(s$coef)

co <- s$coef[, 1]

se <- s$coef[, 2]

pval <- s$coef[, 4]

rs <- s$r.squared

adj <- s$adj.r.squared

sig<-s$sigma ##added it

n <- nobs(model)

gof <- numeric()

gof.names <- character()

gof.decimal <- logical()

if (include.rsquared == TRUE) {

gof <- c(gof, rs)

gof.names <- c(gof.names, "R$^2$")

gof.decimal <- c(gof.decimal, TRUE)

}

if (include.adjrs == TRUE) {

gof <- c(gof, adj)

gof.names <- c(gof.names, "Adj. R$^2$")

gof.decimal <- c(gof.decimal, TRUE)

}

if (include.rmse == TRUE) { ##added it

gof <- c(gof, sig)

gof.names <- c(gof.names, "RMSE")

gof.decimal <- c(gof.decimal, TRUE)

}

if (include.nobs == TRUE) {

gof <- c(gof, n)

gof.names <- c(gof.names, "Num. obs.")

gof.decimal <- c(gof.decimal, FALSE)

}

tr <- createTexreg(coef.names = names, coef = co, se = se,

pvalues = pval, gof.names = gof.names, gof = gof, gof.decimal = gof.decimal)

return(tr)

}

then I runned it in the console and search to definitively override it using

`setMethod("extract", signature=className("lm","stats"), definition=extract.lm, where="package:texreg")`

which is also the way suggested by the author on rforge, but I have this error

`Error in setMethod ...`

the environment "texreg" is locked; cannot assign methods for function

I also tried with fixInNamespace(), but the body function is not showed, even if the source of the function is accessible if you insert its name, or with the body() function, in the terminal (this is the way I accessed and modified it).

Where is my mistake?? How can I make this?

Note:

extract is a S4 generic.

Please,

I want to override definitively the function, in the package environment.

Thank you!

Answer

The error is caused by your attempt to overwrite an object in the package environment. I would suggest creating the method in your local environment, which will be given priority by R when you use `texreg`

in conjunction with the `extract.lm`

method:

```
setMethod("extract", signature = className("lm", "stats"),
definition = extract.lm)
```

See section 6 of the article in the Journal of Statistical Software: http://www.jstatsoft.org/v55/i08/

Source (Stackoverflow)

Comments