[R] coding negative effects in categorical dummy variables

andre74 andre.pfeuffer at web.de
Tue Dec 1 23:55:47 CET 2009


Hello,

I have a problem with categorical variables and dummy encoding. I've a
factor and
for each pair (i,j) with i != j, I'd like to fit 
res ~ a*x[i] - b*x[j]. A brief example with 3 variables:

a - b = 2
b - c = -1
c - a = 0

Thus I fitted the following model: 

fit <- lm(result ~ X + Y) 

where Y is just the negative of X, means y[i] = -x[i]. 

But I don't want to double the variables. Thus I used the coding:

ht <- unclass(X)
at <- unclass(Y)
cnt<-0
for(j in 1:100) # number of equations
{ 
   cnt<-cnt+1
   y[cnt,at[j]]=1;
   y[cnt,ht[j]]=-1;  
} 
 
fit <- lm(res ~ y[-length(y)]) # omit singularities.

>From the equations above 
a - b = 2
b - c = -1
c - a = 0

a  b    c    res
--------------
1 -1    0   2
0  1   -1  -1
-1 0   1    0 

Since this matrix is singular. I omit last column (c) 
and replaced it by intercept:

Change to 
a    b   c(intercept)   res
1   -1   1                 2   
0    1   1                  -1
-1  0    1                 0

and solved this. But somehow this incorrect.
I stuck in this since a couple of while. Does
anybody know how to solve this? Maybe solve.QP 
is an answer to this? 

Please help.... 
-- 
View this message in context: http://n4.nabble.com/coding-negative-effects-in-categorical-dummy-variables-tp932611p932611.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list