[R] Systemfit (was RE: How to force two regression coefficients to be equal but opposite in sign?)

Arne Henningsen arne.henningsen at googlemail.com
Tue Jun 10 08:33:19 CEST 2008

Hi Keith!

On Monday 09 June 2008 16:27, Woolner, Keith wrote:
> [...]
> After I sent my initial message, I came across the Systemfit package,
> which allows specification of constraints on parameters.  In theory,
> this should solve my problem perfectly.  However, I was not able to get
> it to work with my data, as every attempt yielded the following error:
> Error in dimnames(x) <- dn :
>   length of 'dimnames' [2] not equal to array extent
> I suspect that it is related to some of my variables being factors
> rather than numeric.

Yes and no (see below).

> library(systemfit)
> # create data frame - X1, X2, X3, X4 are numeric.  E1 and E2 are factors
> df <- data.frame(
> 	X1 =
> c(.20,.10,.40,.05,.10,.24,.30,.70,.48,.22,.87,.29,.24,.19,.92),
> 	X2 =
> c(.25,.12,.45,.01,.19,.50,.30,.40,.50,.40,.68,.30,.16,.02,.70),
> 	E1 =
> c("A","A","A","B","B","B","C","C","C","D","D","D","E","E","E"),
> 	E2 =
> c("B","C","D","A","D","E","A","B","E","B","C","E","A","B","C")
> )
> df$X3 <- sqrt(df$X1)+runif(1)
> df$X4 <- sqrt(df$X2)+runif(1)
> # Create constraint matrix such that the last two variables must be
> equal but opposite in sign

No. I guess that you mean that the *coefficients* (and not the variables) must 
be equal but opposite in sign.

> tx <- matrix(0,nrow=1,ncol=4)
> tx[1,3]<- 1
> tx[1,4]<- 1
> # Run systemfit with only numeric variables (works)
> systemfit(X2 ~ X1 + X3 + X4,"OLS", data=df, restrict.matrix=tx)
> # Run systemfit with factors but not constraints (works)
> systemfit(X2 ~ X1 + E1 + E2,"OLS", data=df)
> # Run systemfit with factors and constraints (this returns an error)
> systemfit(X2 ~ X1 + E1 + E2,"OLS", data=df, restrict.matrix=tx)

Run this regression without constraints (works)
   systemfit(X2 ~ X1 + E1 + E2,"OLS", data=df )

Take a look at the coefficients: We have *10* coefficients now (because "E1" 
and "E2" are factors. Hence, your restriction matrix must have *10* columns. 
For instance, if you want to restrict the coefficients of the "B"s in "E1" 
and "E2" (third and seventh coefficient, respectively) to be equal but 
opposite in sign, you could do the following:
   tx2 <- matrix(0,nrow=1,ncol=10)
   tx2[1,3]<- 1
   tx2[1,7]<- 1
   systemfit(X2 ~ X1 + E1 + E2,"OLS", data=df, restrict.matrix=tx2)

> Is systemfit able to deal with factors as independent variables having
> constraints, and if so, is there some trick in formulating the problem?
> I searched through the package documentation, but did not see mention of
> factors being either supported or unsupported.

Until now, I thought that it not necessary to say something about factors, 
because they should work in systemfit as in other R functions (e.g. lm).
The documentation says that "restrict.matrix" must be a j x k matrix, where k 
is the number of all parameters (NOT the number of all regressors, which 
differs if some regressors are factors). Hence, I think that the 
documentation is clear enough. However, please tell me if you have any 
suggestions for improving the documentation.

Best wishes,

Arne Henningsen
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080610/0db9e071/attachment.bin>

More information about the R-help mailing list