Re: [R] forcing a zero level in contr.sum
Bond, Stephen wrote: I need to use contr.sum and observe that some levels are not statistically different from the overall mean of zero. What is the proper way of forcing the zero estimate? It seems the column corresponding to that level should become a column of zeros. Is there a way to achieve that without me constructing the design matrix? As Berwin (indirectly) points out, you probably overlooked the how.many argument to C(). However, are you _sure_ that you want to do this? This is like testing that one treatment is exactly equal to the average of all other treatments, which is a rather strange hypothesis. -- Peter Dalgaard Center for Statistics, Copenhagen Business School Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.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] forcing a zero level in contr.sum
Peter, I am asked to show seasonal effects vs a general averaged level without any seasonal effects. That's why contr.sum and not treatment. The estimates show that some coefs are not stat different from 0, which is also the enforced total average. So, my understanding of model selection leads me to force certain seasons to be zero. For example: Y= a0 + b1 + Xb | factor=1 Y= a0 +Xb| factor=2 Y= a0 + b2 + Xb | factor=3 b1+b2=0 | which in this case means b1=-b2 but with more levels not necessarily Stephen Bond | -Original Message- From: Peter Dalgaard [mailto:pda...@gmail.com] Sent: Thursday, July 08, 2010 4:09 AM To: Bond, Stephen Cc: r-help@r-project.org Subject: Re: [R] forcing a zero level in contr.sum Bond, Stephen wrote: I need to use contr.sum and observe that some levels are not statistically different from the overall mean of zero. What is the proper way of forcing the zero estimate? It seems the column corresponding to that level should become a column of zeros. Is there a way to achieve that without me constructing the design matrix? As Berwin (indirectly) points out, you probably overlooked the how.many argument to C(). However, are you _sure_ that you want to do this? This is like testing that one treatment is exactly equal to the average of all other treatments, which is a rather strange hypothesis. -- Peter Dalgaard Center for Statistics, Copenhagen Business School Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.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] forcing a zero level in contr.sum
I need to use contr.sum and observe that some levels are not statistically different from the overall mean of zero. What is the proper way of forcing the zero estimate? It seems the column corresponding to that level should become a column of zeros. Is there a way to achieve that without me constructing the design matrix? Thank you. Stephen Bond [[alternative HTML version deleted]] __ 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] forcing a zero level in contr.sum
On Jul 7, 2010, at 3:13 PM, Bond, Stephen wrote: I need to use contr.sum and observe that some levels are not statistically different from the overall mean of zero. What is the proper way of forcing the zero estimate? It seems the column corresponding to that level should become a column of zeros. Is there a way to achieve that without me constructing the design matrix? Thank you. lm( formula = z ~ x + y + 0, ...) _might_ do something close to what you want. David Winsemius, MD West Hartford, CT __ 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] forcing a zero level in contr.sum
Clarifying my question: options(contrasts = c(contr.sum, contr.poly)) contrasts(fixw$snconv) [,1] [,2] [,3] [,4] [,5] 0110000 0301000 0500100 0600010 0700001 09 -1 -1 -1 -1 -1 I need to force the coefficient on level 03 to be zero. Thank you. Stephen Bond -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, July 07, 2010 3:44 PM To: Bond, Stephen Cc: r-help@r-project.org Subject: Re: [R] forcing a zero level in contr.sum On Jul 7, 2010, at 3:13 PM, Bond, Stephen wrote: I need to use contr.sum and observe that some levels are not statistically different from the overall mean of zero. What is the proper way of forcing the zero estimate? It seems the column corresponding to that level should become a column of zeros. Is there a way to achieve that without me constructing the design matrix? Thank you. lm( formula = z ~ x + y + 0, ...) _might_ do something close to what you want. David Winsemius, MD West Hartford, CT __ 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] forcing a zero level in contr.sum
On Jul 7, 2010, at 4:04 PM, Bond, Stephen wrote: Clarifying my question: options(contrasts = c(contr.sum, contr.poly)) contrasts() [,1] [,2] [,3] [,4] [,5] 0110000 0301000 0500100 0600010 0700001 09 -1 -1 -1 -1 -1 I need to force the coefficient on level 03 to be zero. ?factor ?relevel Perhaps worth a try: fixw$snconv - relevel(fixw$snconv, ref=03) (But I wonder if using contr.sum will ever generally satisfy that goal, since contr.sum calculates the difference from a grand mean and this will only work if a) the GM=0 and b) there is only one term on the RHS of the model, and c) probably a bunch of other restrictions.) -- David. Thank you. Stephen Bond -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, July 07, 2010 3:44 PM To: Bond, Stephen Cc: r-help@r-project.org Subject: Re: [R] forcing a zero level in contr.sum On Jul 7, 2010, at 3:13 PM, Bond, Stephen wrote: I need to use contr.sum and observe that some levels are not statistically different from the overall mean of zero. What is the proper way of forcing the zero estimate? It seems the column corresponding to that level should become a column of zeros. Is there a way to achieve that without me constructing the design matrix? Thank you. lm( formula = z ~ x + y + 0, ...) _might_ do something close to what you want. David Winsemius, MD West Hartford, CT David Winsemius, MD West Hartford, CT __ 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] forcing a zero level in contr.sum
Please, do not post if you do not know the answer. People will see this has answers and skip. I tried with mat1=contrasts(fixw$snconv) mat1=mat1[,-2] summary(frm2sum - glm(resp.frm ~ C(snconv,contr=mat1)+mprime+mshape,data=fixw,family=quasibinomial)) the unwanted level is still there. Unbelievable. Stephen Bond -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, July 07, 2010 4:15 PM To: Bond, Stephen Cc: r-help@r-project.org Subject: Re: [R] forcing a zero level in contr.sum On Jul 7, 2010, at 4:04 PM, Bond, Stephen wrote: Clarifying my question: options(contrasts = c(contr.sum, contr.poly)) contrasts() [,1] [,2] [,3] [,4] [,5] 0110000 0301000 0500100 0600010 0700001 09 -1 -1 -1 -1 -1 I need to force the coefficient on level 03 to be zero. ?factor ?relevel Perhaps worth a try: fixw$snconv - relevel(fixw$snconv, ref=03) (But I wonder if using contr.sum will ever generally satisfy that goal, since contr.sum calculates the difference from a grand mean and this will only work if a) the GM=0 and b) there is only one term on the RHS of the model, and c) probably a bunch of other restrictions.) -- David. Thank you. Stephen Bond -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, July 07, 2010 3:44 PM To: Bond, Stephen Cc: r-help@r-project.org Subject: Re: [R] forcing a zero level in contr.sum On Jul 7, 2010, at 3:13 PM, Bond, Stephen wrote: I need to use contr.sum and observe that some levels are not statistically different from the overall mean of zero. What is the proper way of forcing the zero estimate? It seems the column corresponding to that level should become a column of zeros. Is there a way to achieve that without me constructing the design matrix? Thank you. lm( formula = z ~ x + y + 0, ...) _might_ do something close to what you want. David Winsemius, MD West Hartford, CT David Winsemius, MD West Hartford, CT __ 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] forcing a zero level in contr.sum
Please do not post with ambiguous questions lacking sufficient R code and data to represent the problem. On Jul 7, 2010, at 4:41 PM, Bond, Stephen wrote: Please, do not post if you do not know the answer. People will see this has answers and skip. More likely people will see that it has insufficient detail to answer ,,, and then also skip. You still have not provided any insight into the structure of fixw. I tried with mat1=contrasts(fixw$snconv) mat1=mat1[,-2] summary(frm2sum - glm(resp.frm ~ C(snconv,contr=mat1)+mprime +mshape,data=fixw,family=quasibinomial)) the unwanted level is still there. But did you try: summary(frm2sum - glm(resp.frm ~ relevel(snconv, ref=03) + mprime + mshape, data=fixw, family=quasibinomial)) (The above obviously not tested on fixw in the absence of a reproducible example, but was an analog modulo the minimal information offered was tested on the quasipoisson example in help page of family.) -- David. Unbelievable. snarky comment deleted Stephen Bond -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, July 07, 2010 4:15 PM To: Bond, Stephen Cc: r-help@r-project.org Subject: Re: [R] forcing a zero level in contr.sum On Jul 7, 2010, at 4:04 PM, Bond, Stephen wrote: Clarifying my question: options(contrasts = c(contr.sum, contr.poly)) contrasts() [,1] [,2] [,3] [,4] [,5] 0110000 0301000 0500100 0600010 0700001 09 -1 -1 -1 -1 -1 I need to force the coefficient on level 03 to be zero. ?factor ?relevel Perhaps worth a try: fixw$snconv - relevel(fixw$snconv, ref=03) (But I wonder if using contr.sum will ever generally satisfy that goal, since contr.sum calculates the difference from a grand mean and this will only work if a) the GM=0 and b) there is only one term on the RHS of the model, and c) probably a bunch of other restrictions.) -- David. Thank you. Stephen Bond -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, July 07, 2010 3:44 PM To: Bond, Stephen Cc: r-help@r-project.org Subject: Re: [R] forcing a zero level in contr.sum On Jul 7, 2010, at 3:13 PM, Bond, Stephen wrote: I need to use contr.sum and observe that some levels are not statistically different from the overall mean of zero. What is the proper way of forcing the zero estimate? It seems the column corresponding to that level should become a column of zeros. Is there a way to achieve that without me constructing the design matrix? Thank you. lm( formula = z ~ x + y + 0, ...) _might_ do something close to what you want. David Winsemius, MD West Hartford, CT David Winsemius, MD West Hartford, CT __ 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. David Winsemius, MD West Hartford, CT __ 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] forcing a zero level in contr.sum
If I surmise correctly, you want something like contr.sum but with the option to select the level with the row of '-1' and no column. Here '3' is that level: contr.sum(3) [,1] [,2] 110 201 3 -1 -1 relevel() and contr.sum() generally will not do what you want without some awful twists and turns, I think. Better to roll your own contrast function like this: contr.mysum - function (n, contrasts = TRUE, sparse = FALSE, base=length(levels)) { if (length(n) = 1L) { if (is.numeric(n) length(n) == 1L n 1L) levels - seq_len(n) else stop(not enough degrees of freedom to define contrasts) } else levels - n levels - as.character(levels) cont - .Diag(levels, sparse = sparse) if (contrasts) { cont - cont[, -base, drop = FALSE] cont[base, ] - -1 colnames(cont) - NULL } cont } # now pick the base you want: # Here is a reproducible example lm(weight~C(Diet,contr.mysum),ChickWeight) # default like contr.sum lm(weight~C(Diet,contr.mysum,base=1),ChickWeight) lm(weight~C(Diet,contr.mysum,base=2),ChickWeight) lm(weight~C(Diet,contr.mysum,base=3),ChickWeight) lm(weight~C(Diet,contr.mysum,base=4),ChickWeight) Next time, please do set up code others can run as David W. requested. See library(help='datasets') for good example datasets or see the posting guide for details on how to include your own data sets. Chuck p.s. I have never noticed that posters on this list stop answering a question just because others have already answered it. Try asking a question like 'How do I use a regex to solve this problem?' and start counting the responses. ;-) On Wed, 7 Jul 2010, David Winsemius wrote: Please do not post with ambiguous questions lacking sufficient R code and data to represent the problem. On Jul 7, 2010, at 4:41 PM, Bond, Stephen wrote: Please, do not post if you do not know the answer. People will see this has answers and skip. More likely people will see that it has insufficient detail to answer ,,, and then also skip. You still have not provided any insight into the structure of fixw. I tried with mat1=contrasts(fixw$snconv) mat1=mat1[,-2] summary(frm2sum - glm(resp.frm ~ C(snconv,contr=mat1)+mprime+mshape,data=fixw,family=quasibinomial)) the unwanted level is still there. But did you try: summary(frm2sum - glm(resp.frm ~ relevel(snconv, ref=03) + mprime + mshape, data=fixw, family=quasibinomial)) (The above obviously not tested on fixw in the absence of a reproducible example, but was an analog modulo the minimal information offered was tested on the quasipoisson example in help page of family.) -- David. Unbelievable. snarky comment deleted Stephen Bond -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, July 07, 2010 4:15 PM To: Bond, Stephen Cc: r-help@r-project.org Subject: Re: [R] forcing a zero level in contr.sum On Jul 7, 2010, at 4:04 PM, Bond, Stephen wrote: Clarifying my question: options(contrasts = c(contr.sum, contr.poly)) contrasts() [,1] [,2] [,3] [,4] [,5] 0110000 0301000 0500100 0600010 0700001 09 -1 -1 -1 -1 -1 I need to force the coefficient on level 03 to be zero. ?factor ?relevel Perhaps worth a try: fixw$snconv - relevel(fixw$snconv, ref=03) (But I wonder if using contr.sum will ever generally satisfy that goal, since contr.sum calculates the difference from a grand mean and this will only work if a) the GM=0 and b) there is only one term on the RHS of the model, and c) probably a bunch of other restrictions.) -- David. Thank you. Stephen Bond -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, July 07, 2010 3:44 PM To: Bond, Stephen Cc: r-help@r-project.org Subject: Re: [R] forcing a zero level in contr.sum On Jul 7, 2010, at 3:13 PM, Bond, Stephen wrote: I need to use contr.sum and observe that some levels are not statistically different from the overall mean of zero. What is the proper way of forcing the zero estimate? It seems the column corresponding to that level should become a column of zeros. Is there a way to achieve that without me constructing the design matrix? Thank you. lm( formula = z ~ x + y + 0, ...) _might_ do something close to what you want. David Winsemius, MD West Hartford, CT David Winsemius, MD West Hartford, CT __ 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. David Winsemius, MD West Hartford, CT __ R-help@r-project.org
Re: [R] forcing a zero level in contr.sum
G'day Stephen, On Wed, 7 Jul 2010 16:41:18 -0400 Bond, Stephen stephen.b...@cibc.com wrote: Please, do not post if you do not know the answer. People will see this has answers and skip. I tried with mat1=contrasts(fixw$snconv) mat1=mat1[,-2] summary(frm2sum - glm(resp.frm ~ C(snconv,contr=mat1)+mprime+mshape,data=fixw,family=quasibinomial)) the unwanted level is still there. Unbelievable. model.matrix() instead of summary() would reveal that the estimate corresponding to the unwanted level is probably estimating something quite different then what you think it estimates. ?C and look at the how.many argument, presumably you want C(snconv, contr=mat1, how.many=NCOL(mat1)) To see what is going on, consider: R ( tmp - gl(4,3) ) [1] 1 1 1 2 2 2 3 3 3 4 4 4 Levels: 1 2 3 4 R options(contrasts=c(contr.sum, contr.poly)) R ( tt - contrasts(tmp) ) [,1] [,2] [,3] 1100 2010 3001 4 -1 -1 -1 R tt - tt[,-2] R contrasts(tmp, 2) - tt R tmp [1] 1 1 1 2 2 2 3 3 3 4 4 4 attr(,contrasts) [,1] [,2] 110 200 301 4 -1 -1 Levels: 1 2 3 4 R contrasts(tmp) - tt R tmp [1] 1 1 1 2 2 2 3 3 3 4 4 4 attr(,contrasts) [,1] [,2] [,3] 110 0.2886751 200 -0.8660254 301 0.2886751 4 -1 -1 0.2886751 Levels: 1 2 3 4 HTH. Cheers, Berwin == Full address Berwin A Turlach Tel.: +61 (8) 6488 3338 (secr) School of Maths and Stats (M019)+61 (8) 6488 3383 (self) The University of Western Australia FAX : +61 (8) 6488 1028 35 Stirling Highway Crawley WA 6009e-mail: ber...@maths.uwa.edu.au Australiahttp://www.maths.uwa.edu.au/~berwin __ 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] forcing a zero level in contr.sum
On Wed, Jul 7, 2010 at 10:55 PM, Charles C. Berry cbe...@tajo.ucsd.edu wrote: contr.mysum - function (n, contrasts = TRUE, sparse = FALSE, base=length(levels)) { The memisc package has a contr.sum function with a base= argument. __ 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.