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

From: Woolner, Keith <kwoolner_at_indians.com>
Date: Tue, 10 Jun 2008 09:35:40 -0400

Thank you Arne. Though I didn't detail all of my attempts, I did in fact try a larger restriction matrix that accounted for the levels of the factors.

However, I think my error in those attempts was in adding N columns to the matrix for an N-level factor (rather than N-1 columns), because I thought I needed to explicitly constrain all 5 coefficients of each factor. I am aware of how lm() returns coefficients for all but the first level of the factor, but wasn't clear on how that interacted with the restriction matrix.

A brief sentence in the documentation emphasizing the relationship of the number of levels of a factor to the number of coefficients represented in the restriction matrix would have helped me a lot in this situation, but I would, of course, defer to you as to whether this tip is of general enough interest to include in the documentation.

Thank you again for your patience and assistance.

Keith

> -----Original Message-----
> From: Arne Henningsen [mailto:arne.henningsen_at_googlemail.com]
> Sent: Tuesday, June 10, 2008 2:33 AM
> To: r-help@r-project.org
> Cc: Woolner, Keith
> Subject: Re: [R] Systemfit (was RE: How to force two regression
> coefficients to be equal but opposite in sign?)
>
> 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
>
> --
> Arne Henningsen
> http://www.arne-henningsen.name



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 10 Jun 2008 - 13:41:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 10 Jun 2008 - 14:30:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive