[R] Model frame manipulation

From: Millo Giovanni <Giovanni_Millo_at_generali.com>
Date: Tue 06 Jul 2004 - 18:04:08 EST


Dear all,

I am implementing a redundant variables F-test. For that I need to compute 2 models, restricted and unrestricted, then extracting the residuals to calculate the test statistic.

I borrowed this elegant solution from the LMtest package to rebuild the first of the matrices involved (the unrestricted one) on the basis of model spec. and data

>red.var.test<-function(formula, red.vars, data = list()) {
> mf <- model.frame(formula, data = data)
> y <- model.response(mf)
> modelterms <- terms(formula, data = data)
> X <- model.matrix(modelterms, data = data) # unrestricted model
matrix
>
> n <- nrow(X)
> k <- ncol(X)
>

...but then I had to resort to this solution of mine to select the restricted m.m. according to the possibly redundant regressors specified in the character vector red.vars

> Z<-X[,!(dimnames(X)[[2]]%in%red.vars)] # restricted model matrix
>

and then the rest...

> q<-dim(X)[[2]]-dim(Z)[[2]]
>
> umod<-lm(y~X)
> rmod<-lm(y~Z)
>
> ures<-umod$resid
> rres<-rmod$resid
>
> URSS<-sum(ures^2)
> RRSS<-sum(rres^2)
>
> test <- ((RRSS - URSS)/q) / (URSS / (n-k))
> (...) }

Now to my question: the above works just fine when the names in red.vars are *exactly* those of regressors in the model frame, e.g. if I am taking logs of one variable, say, "a", in model mod<-lm(d ~ log(a) + b + c), testing joint redundancy of a and b I have to write

red.var.test(mod, c("log(a)","b"))

Is there a generic way to restrict the model matrix, so as to write only the names of regressors involved without having to bother about the transformations, such as

red.var.test(mod, c("a","b"))

maybe in the style of the first rows?

Thank you in advance for your insights
Giovanni

Giovanni Millo
R&D Dept.
Assicurazioni Generali SpA
Trieste, Italy
Ai sensi del D.Lgs. 196/2003 si precisa che le informazioni contenute in questo messaggio sono riservate ed a uso esclusivo del destinatario. Qualora il messaggio in parola Le fosse pervenuto per errore, La invitiamo ad eliminarlo senza copiarlo e a non inoltrarlo a terzi, dandocene gentilmente comunicazione. Grazie.<BR><BR>Pursuant to Legislative Decree No. 196/2003, you are hereby informed that this message contains confidential information intended only for the use of the addressee. If you are not the addressee, and have received this message by mistake, please delete it and immediately notify us. You may not copy or disseminate this message to anyone. Thank you.

        [[alternative HTML version deleted]]



R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jul 06 18:08:28 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:54:43 EST