Skip to content
Prev 140000 / 398502 Next

two cols in a data frame are the same factor

Here is one approach:

First run a regular lm command without the restrictions, but specify
y=TRUE, x=TRUE.

This will do the unconstrained regression, but part of the return object
will be the y variable after subsetting, NA removal, etc. and the x
matrix that was used, this x matrix will have your 2 factors converted
into indicator/dummy variables (along with any other covariates
mentioned).   Take the x and y components of that return and put them
into a new data frame.

Now do a regression using the new data frame as your data and include
I(f1.1+f2.1) terms just like you would with numeric predictors to force
the coefficients to be equal.

You could also accomplish the same idea in the original regression using
a formula like:

Y ~ I( fac1=='A' + fac2=='A' ) + I( fac1=='B' + fac2=='B' ) + ...

For each level (other than the baseline level, or including it if you
leave out the intercept) of fac1 and fac2.  Both do essentially the same
thing, create your own set of indicator variables rather than depending
on R to do it.

Hope this helps,