[R] limited formula length in tsls

John Fox jfox at mcmaster.ca
Thu Aug 23 01:51:47 CEST 2001


Dear Zsombor,

I believe that tsls.formula is from my sem package. What you list as the 
"original source" and your modified version appear to be reversed.

The reason that the original version manipulates (somewhat awkwardly) the 
model and instrumental-variables formulas in character form to produce a 
combined formula is to insure that missing data get treated consistently; 
this will not be the case, I believe, in your modified version. (I note, by 
the way, that you seem to have added a function for weighted two-stage 
least-squares estimation.)

The source of the problem appears to be that when model formulas are 
translated into character, the right-hand-side of the model gets truncated 
at (I think) 68 characters. Consider the following example:

 > mod <- this ~ is + a + very + long + formula + with + a + very + large + 
number + of + characters
 > mod
this ~ is + a + very + long + formula + with + a + very + large +
     number + of + characters
 > as.character(mod)
[1] "~"
[2] "this"
[3] "is + a + very + long + formula + with + a + very + large + number + "
 >

Clearly, the current situation is unsatisfactory, but it's unclear to me 
what the best course of action is. A more elegant approach to treating the 
model formula and instruments consistently would solve the problem, but 
as.character should probably not truncate formulas.

John



At 06:25 PM 22/08/2001 +0100, Zsombor Cseres-Gergely wrote:
>Dear all,
>
>Using the tsls package, I noticed that regression lists longer
>than 64 character
>are getting truncated. Looking at the original source,
>
>tsls.formula <- function(model, instruments, data, subset, weights,
>     na.action, contrasts=NULL){
>     if (missing(na.action))
>         na.action <- options()$na.action
>     m <- match.call(expand.dots = FALSE)
>     if (is.matrix(eval(m$data, sys.frame(sys.parent()))))
>         m$data <- as.data.frame(data)
>     m$formula <- model
>     m$instruments <- m$model <- m$contrasts <- NULL
>     m[[1]] <- as.name("model.frame")
>     mf <- eval(m, sys.frame(sys.parent()))
>     na.act <- attr(mf, "na.action")
>     Z <- model.matrix(instruments, data, contrasts)
>     response <- attr(attr(mf, "terms"), "response")
>     y <- model.response(mf)
>     X <- model.matrix(model, mf, contrasts)
>     w <- model.weights(mf)
>     if (missing(weights)) result <- tsls(y, X, Z, colnames(X))
>     else result <- tsls.weighted(y, X, Z, w, colnames(X))
>     result$formula <- model
>     result$instruments <- instruments
>     if (!is.null(na.act))
>         result$na.action <- na.act
>     class(result) <- "tsls"
>     result
>     }
>
>
>I thought that the problem here is to handle the instruments correctly and
>being consistent with the convention (?) regarding the single model object
>inside the function. Since I could not figure out, why the truncation occurs
>(I tried as.character() and paste() in interactive mode, but nothing bad
>happened), I modified the source ad follows:
>
>
>tsls.formula <- function(model, instruments, data, subset,
>     na.action, contrasts=NULL){
>     if (missing(na.action))
>         na.action <- options()$na.action
>     m <- match.call(expand.dots = FALSE)
>     if (is.matrix(eval(m$data, sys.frame(sys.parent()))))
>         m$data <- as.data.frame(data)
>     c1 <- as.character(model)
>     c2 <- as.character(instruments)
>     formula <- as.formula(paste(c1[2],c1[1],c1[3], '+', c2[2]))
>     m$formula <- formula
>     m$instruments <- m$model <- m$contrasts <- NULL
>     m[[1]] <- as.name("model.frame")
>     mf <- eval(m, sys.frame(sys.parent()))
>     na.act <- attr(mf, "na.action")
>     Z <- model.matrix(instruments, data = mf, contrasts)
>     response <- attr(attr(mf, "terms"), "response")
>     y <- mf[,response]
>     X <- model.matrix(model, data=mf, contrasts)
>     result <- tsls(y, X, Z, colnames(X))
>     result$response.name <- c1[2]
>     result$formula <- model
>     result$instruments <- instruments
>     if (!is.null(na.act))
>         result$na.action <- na.act
>     class(result) <- "tsls"
>     result
>     }
>
>It works, but I feel that there is something wrong with it (although
>it works fine so far). What?
>
>Thank you,
>Zsombor Cseres-Gergely
>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
>r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
>Send "info", "help", or "[un]subscribe"
>(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list