[R] Problem with systemfit 0.7-3 and transformed variables

Mikko Pakkanen mikko.pakkanen at helsinki.fi
Wed May 25 18:25:12 CEST 2005


> We did not notice this shortcoming of systemfit() so far. Unfortunately,
> I 
> don't have the time in the next few days to look into the code and figure
> out 
> how to enable transformed variables. I suggest that you either create 
> transformed variables by hand or you modify the systemfit code to enable
> this 
> and send us the patch. I prefer the second :-) (that's the philosophy of
> open-source software like R: useRs become developeRs).

Luckily, I had some time to check the code. Debugger revealed that the
problems are caused by the model.frame function which is used to compile the
'$data' data frame. I don't need that data frame so much, so I just
substituted model.frame with model.matrix which apparently doesn't cause
this error with transformed variables. However, I tuned it a bit further, so
that it should still return an identical '$data' data frame, despite the
modification.
I've only tested this with my example and it appears to be OK. Still, I
think this should be considered a "quick & dirty" fix -there are probably
better ways to do it. But, I hope it gives the idea. Here's my attempt:

mikko at briscoe R $ diff -u systemfit.R systemfit-patched.R
--- systemfit.R 2004-11-26 11:17:36.000000000 +0200
+++ systemfit-patched.R 2005-05-25 18:55:55.568944699 +0300
@@ -624,7 +624,11 @@
     Terms <- terms( eqns[[i]], data = data)
     m$formula <- Terms
     m <- eval(m, parent.frame())
-    datai <- model.frame(Terms, m)
+    resp <- model.extract(m, "response")
+       ## using model.matrix instead of model.frame, need to get the output
variable separately
+    datai <- data.frame(cbind(resp, (model.matrix(Terms, m))[,-1]))
+       ## I guess there's a better way to extract the name of the output
variable?
+       names(datai)[1] <- as.character(terms(eqns[[i]]))[2]
     if(method=="2SLS" | method=="3SLS") {
       #datai <- cbind( datai, model.frame( instl[[i]] ))
       # the following lines have to be substituted for the previous
@@ -634,7 +638,8 @@
       Terms <- terms(instl[[i]], data = data)
       m$formula <- Terms
       m <- eval(m, parent.frame())
-      datai <- cbind( datai, model.frame(Terms, m))
+      ## used previously model.frame
+      datai <- cbind( datai, as.data.frame((model.matrix(Terms, m))[,-1]))
     }

     if(i==1) {

Regards,

-Mikko.




More information about the R-help mailing list