hosking@watson.ibm.com
2002-Sep-18 21:06 UTC
contrasts in lm~-1+(numeric.variable)/(factor) (PR#2037)
Full_Name: J. R. M. Hosking Version: 1.5 OS: Windows 2000 Submission from: (NULL) (198.81.209.17) Here is a test case: # Some arbitrary data # v1<-1:12 v2<-cumsum(v1) v3<-cumsum(v2) f<-factor(rep(c("a","b","c"),4)) y<-c(1,4,2,7,5,8,7,9,6,10,12,10) print(cbind(y,f,v1,v2,v3)) # # Fit a regression model with no intercept, and different slopes # for each value of the factor # ans<-lm( y ~ -1 + v1/f + v2/f + v3/f, contrasts=list(f=contr.sum)) print(as.matrix(ans$coefficients)) And its output:> # Some arbitrary data > # > v1<-1:12 > v2<-cumsum(v1) > v3<-cumsum(v2) > f<-factor(rep(c("a","b","c"),4)) > y<-c(1,4,2,7,5,8,7,9,6,10,12,10) > print(cbind(y,f,v1,v2,v3))y f v1 v2 v3 [1,] 1 1 1 1 1 [2,] 4 2 2 3 4 [3,] 2 3 3 6 10 [4,] 7 1 4 10 20 [5,] 5 2 5 15 35 [6,] 8 3 6 21 56 [7,] 7 1 7 28 84 [8,] 9 2 8 36 120 [9,] 6 3 9 45 165 [10,] 10 1 10 55 220 [11,] 12 2 11 66 286 [12,] 10 3 12 78 364> # > # Fit a regression model with no intercept, and different slopes > # for each value of the factor > # > ans<-lm( y ~ -1 + v1/f + v2/f + v3/f, contrasts=list(f=contr.sum)) > print(as.matrix(ans$coefficients))[,1] v1 1.5927715155 v2 -0.5228148121 v3 0.0718702660 v1:fa 1.3916372272 v1:fb 0.5747198574 v1:fc NA f1:v2 -0.2937603407 f2:v2 0.0328886311 f1:v3 0.0414907636 f2:v3 0.0003204578 Though I asked for "sum" contrasts, the v1 coefficients came back with "treatment" contrasts. I don't see any technical reason why "sum" contrasts can't be computed. Is it a bug? Also, why should the order of the variables be reversed (f1:v2 where I would have expected v2:f1, etc.)? By the way, S-plus refuses to fit this model at all ("Problem in model.matrix.default(Terms, m, contrasts): Internal error: predicted 2 columns for f %in% v1, got 3). Version information: _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 1 minor 5.1 year 2002 month 06 day 17 language R -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Seemingly Similar Threads
- gls yields much smaller std. errors with different base for contrasts
- anova interaction contrasts: crossing helmert and linear contrasts
- documentation for contrasts and contrasts<- (PR#607)
- Different results for sparse and dense version of model matrix using contrasts and interactions
- model.matrix.default() silently ignores bad contrasts.arg