[R] Change the order of variables in a linear model
Hello, the following lines m <- matrix(c(1,1,9,1,2,6,1,3,7,2,1,4,2,2,5,2,3,1,3,1,2,3,2,-1,3,3,-2), 9, 3, byrow = TRUE, dimnames=list(NULL, cbind('A','B','Y'))) md <- as.data.frame(m) md$A <- as.factor(md$A) md$B <- as.factor(md$B) mm <- model.matrix(Y~A+B+A:B, data=md) produce > mm (Intercept) A2 A3 B2 B3 A2:B2 A3:B2 A2:B3 A3:B3 1 1 0 0 0 0 0 0 0 0 2 1 0 0 1 0 0 0 0 0 3 1 0 0 0 1 0 0 0 0 4 1 1 0 0 0 0 0 0 0 5 1 1 0 1 0 1 0 0 0 6 1 1 0 0 1 0 0 1 0 7 1 0 1 0 0 0 0 0 0 8 1 0 1 1 0 0 1 0 0 9 1 0 1 0 1 0 0 0 1 attr(,"assign") [1] 0 1 1 2 2 3 3 3 3 attr(,"contrasts") attr(,"contrasts")$A [1] "contr.treatment" attr(,"contrasts")$B [1] "contr.treatment" However, instead of the order (Intercept) A2 A3 B2 B3 A2:B2 A3:B2 A2:B3 A3:B3 | | changed order i'd like to have | | (Intercept) A2 A3 B2 B3 A2:B2 A2:B3 A3:B2 A3:B3 that is, the order of the A:B interaction variables is changed. Is there a way to freely position variables in a model? Thank you, Frank -- View this message in context: http://r.789695.n4.nabble.com/Change-the-order-of-variables-in-a-linear-model-tp4630230.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] set specific contrasts using lapply
I have the following data set > data A B X1 X2 Y 1 A1 B1 1.1 2.9 1.2 2 A1 B2 1.0 3.2 2.3 3 A2 B1 1.0 3.3 1.6 4 A2 B2 0.5 2.6 3.1 > sapply(data, class) A BX1X2 Y "factor" "factor" "numeric" "numeric" "numeric" I'd like to set a specific type of contrasts to all the categorical factors (here A and B). In a loop, this can be done like this: for (i in 1:length(data)) { if (class(data[[i]]) == 'factor') { contrasts(data[[i]], length(levels(data[[i]]))) <- contr.treatment(levels(data[[i]]), contrast=FALSE); } } Can i do this without the loop? I tried to use the function set.contrasts <- function(x) { if (class(x) == 'factor') { contrasts(x, length(levels(x))) <- contr.treatment(levels(x), contrast=FALSE); } } in lapply(data, set.contrasts) However, it doesn't work. Any ideas? Thank You -- View this message in context: http://r.789695.n4.nabble.com/set-specific-contrasts-using-lapply-tp4626289.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] generalized inverse using matinv (Design)
thank you all. i have deliberately chosen matinv (although obviously an outdated version) because it uses the sweep operator. i know of other methods to calculate generalized inverses. however, it is also true that the sweep operator is capable of computing g2 generalized inverses. The ginv returns the Moore-Penrose inverse, which has nicer numerical properties if computed by svd, but it is just not what i want. (besides, svd is very slow and should not be applied to the normal equations anyway) so lets explain my peculiar interest in matinv: i am trying get my head around the type III SS that SAS popularized. i know that those hypotheses are hated in the R community but they are the standard hypothesis given by all other statistics software. i know how to compute type III SS and how to translate them to meaningful hypothesis of the cell means model. but i do not have an intuitive understanding why they are computed as they are. so i came across my matinv problem when i tried to to compute the generating matrix _ H = (X'X) X'X used for the construction of estimable functions. if the g2-inverse is used, then H has canonical form which simpifies the interpretion. i just wanted to check if it is invariant to cell frequencies. -- View this message in context: http://r.789695.n4.nabble.com/generalized-inverse-using-matinv-Design-tp3747337p3749373.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] generalized inverse using matinv (Design)
i am trying to use matinv from the Design package to compute the generalized inverse of the normal equations of a 3x3 design via the sweep operator. That is, for the linear model y = ยต + x1 + x2 + x1*x2 where x1, x2 are 3-level factors and dummy coding is being used the matrix to be inverted is X'X = 9 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 3 3 0 0 1 1 1 1 0 0 1 0 0 1 0 0 3 0 3 0 1 1 1 0 1 0 0 1 0 0 1 0 3 0 0 3 1 1 1 0 0 1 0 0 1 0 0 1 3 1 1 1 3 0 0 1 1 1 0 0 0 0 0 0 3 1 1 1 0 3 0 0 0 0 1 1 1 0 0 0 3 1 1 1 0 0 3 0 0 0 0 0 0 1 1 1 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1 this matrix has rank=9 however, matinv(X'X) falsely returns rank=4, no matter what the tolerance threshold eps is set to. also the defining property of the generalized inverse _ X'X %*% (X'X) %*% X'X = X'X is not satisfied. if i use qr (from the base package) the rank is correctly determined as 9. any ideas? thank you -- View this message in context: http://r.789695.n4.nabble.com/generalized-inverse-using-matinv-Design-tp3747337p3747337.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.