John Sorkin
2011-Oct-28 18:16 UTC
[R] Contrasts with an interaction. How does one specify the dummy variables for the interaction
Forgive my resending this post. To data I have received only one response (thank you Bert Gunter), and I still do not have an answer to my question. Respectfully, John Windows XP R 2.12.1 contrast package. I am trying to understand how to create contrasts for a model that contatains an interaction. I can get contrasts to work for a model without interaction, but not after adding the interaction. Please see code below. The last two contrast statements show the problem. I would appreciate someone letting me know what is wrong with the syntax of my contrast statements. Thank you, John library(contrast) # Create 2x2 contingency table. counts=c(50,50,30,70) row <- gl(2,2,4) column <- gl(2,1,4) mydata <- data.frame(row,column,counts) print(mydata) # Show levels of 2x2 table levels(mydata$row) levels(mydata$column) # Models, no interaction, and interaction fitglm0 <- glm(counts ~ row + column, family=poisson(link="log")) fitglm <- glm(counts ~ row + column + row*column, family=poisson(link="log")) # Contrasts for model without interaction works fine! anova(fitglm0) summary(fitglm0) con0<-contrast(fitglm0,list(row="1",column="1")) print(con0,X=TRUE) # Contrast for model with interaction does not work. anova(fitglm) summary(fitglm) con<-contrast(fitglm,list(row="1",column="1") print(con,X=TRUE) # Nor does this work. con<-contrast(fitglm,list(row="1",column="1",row:column=c("0","0"))) print(con,X=TRUE) John David Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) Confidentiality Statement: This email message, including any attachments, is for th...{{dropped:6}}
Daniel Malter
2011-Oct-28 20:02 UTC
[R] Contrasts with an interaction. How does one specify the dummy variables for the interaction
Is there a specific reason why you insist on using the contrast library? If not: # Create 2x2 contingency table. counts=c(50,50,30,70) row <- gl(2,2,4) column <- gl(2,1,4) mydata <- data.frame(row,column,counts) print(mydata) #Create contrasts row<-factor(row) column<-factor(column) contrasts(row)<-contr.treatment(levels(row)) contrasts(column)<-contr.treatment(levels(column)) # Works for Terps fit.terp<-glm(counts ~ row + column + row*column, family=poisson(link="log")) summary(fit.terp) HTH, Daniel Malter University of Maryland, College Park John Sorkin wrote:> > Forgive my resending this post. To data I have received only one response > (thank you Bert Gunter), and I still do not have an answer to my question. > Respectfully, > John > > > Windows XP > R 2.12.1 > contrast package. > > > I am trying to understand how to create contrasts for a model that > contatains an interaction. I can get contrasts to work for a model without > interaction, but not after adding the interaction. Please see code below. > The last two contrast statements show the problem. I would appreciate > someone letting me know what is wrong with the syntax of my contrast > statements. > Thank you, > John > > > library(contrast) > > # Create 2x2 contingency table. > counts=c(50,50,30,70) > row <- gl(2,2,4) > column <- gl(2,1,4) > mydata <- data.frame(row,column,counts) > print(mydata) > > # Show levels of 2x2 table > levels(mydata$row) > levels(mydata$column) > > > # Models, no interaction, and interaction > fitglm0 <- glm(counts ~ row + column, > family=poisson(link="log")) > fitglm <- glm(counts ~ row + column + row*column, > family=poisson(link="log")) > > # Contrasts for model without interaction works fine! > anova(fitglm0) > summary(fitglm0) > con0<-contrast(fitglm0,list(row="1",column="1")) > print(con0,X=TRUE) > > # Contrast for model with interaction does not work. > anova(fitglm) > summary(fitglm) > con<-contrast(fitglm,list(row="1",column="1") > print(con,X=TRUE) > > # Nor does this work. > con<-contrast(fitglm,list(row="1",column="1",row:column=c("0","0"))) > print(con,X=TRUE) > > > > > John David Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > Confidentiality Statement: > This email message, including any attachments, is for th...{{dropped:6}} > > ______________________________________________ > R-help@ 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. >-- View this message in context: http://r.789695.n4.nabble.com/Contrasts-with-an-interaction-How-does-one-specify-the-dummy-variables-for-the-interaction-tp3948792p3949071.html Sent from the R help mailing list archive at Nabble.com.
John Sorkin
2011-Oct-28 20:36 UTC
[R] Contrasts with an interaction. How does one specify the dummy variables for the interaction
Daniel, I want to use the contrast library because I want to be able to specify any arbitrary post-hoc contrast, e.g. Given a 3x2 table describing smoking (never, former, current) by sex (male,female), I can use a post-hoc contrast to compare the fraction of female former smokers to the fraction of male former smokers, or the fraction of male former smokers to the fraction of male current smokers, etc. To the best of my knowledge, post-hoc contrasts are the most flexible, and easiest way to specify arbitrary pre-specified comparisons. John John David Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)>>> Daniel Malter <daniel at umd.edu> 10/28/2011 4:02 PM >>>Is there a specific reason why you insist on using the contrast library? If not: # Create 2x2 contingency table. counts=c(50,50,30,70) row <- gl(2,2,4) column <- gl(2,1,4) mydata <- data.frame(row,column,counts) print(mydata) #Create contrasts row<-factor(row) column<-factor(column) contrasts(row)<-contr.treatment(levels(row)) contrasts(column)<-contr.treatment(levels(column)) # Works for Terps fit.terp<-glm(counts ~ row + column + row*column, family=poisson(link="log")) summary(fit.terp) HTH, Daniel Malter University of Maryland, College Park John Sorkin wrote:> > Forgive my resending this post. To data I have received only one response > (thank you Bert Gunter), and I still do not have an answer to my question. > Respectfully, > John > > > Windows XP > R 2.12.1 > contrast package. > > > I am trying to understand how to create contrasts for a model that > contatains an interaction. I can get contrasts to work for a model without > interaction, but not after adding the interaction. Please see code below. > The last two contrast statements show the problem. I would appreciate > someone letting me know what is wrong with the syntax of my contrast > statements. > Thank you, > John > > > library(contrast) > > # Create 2x2 contingency table. > counts=c(50,50,30,70) > row <- gl(2,2,4) > column <- gl(2,1,4) > mydata <- data.frame(row,column,counts) > print(mydata) > > # Show levels of 2x2 table > levels(mydata$row) > levels(mydata$column) > > > # Models, no interaction, and interaction > fitglm0 <- glm(counts ~ row + column, > family=poisson(link="log")) > fitglm <- glm(counts ~ row + column + row*column, > family=poisson(link="log")) > > # Contrasts for model without interaction works fine! > anova(fitglm0) > summary(fitglm0) > con0<-contrast(fitglm0,list(row="1",column="1")) > print(con0,X=TRUE) > > # Contrast for model with interaction does not work. > anova(fitglm) > summary(fitglm) > con<-contrast(fitglm,list(row="1",column="1") > print(con,X=TRUE) > > # Nor does this work. > con<-contrast(fitglm,list(row="1",column="1",row:column=c("0","0"))) > print(con,X=TRUE) > > > > > John David Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > Confidentiality Statement: > This email message, including any attachments, is for ...{{dropped:31}}
Mark Difford
2011-Oct-29 07:27 UTC
[R] Contrasts with an interaction. How does one specify the dummy variables for the interaction
John, There is a good example of one way of doing this in "multcomp-examples.pdf" of package multcomp. See pages 8 to 10. Regards, Mark. ----- Mark Difford (Ph.D.) Research Associate Botany Department Nelson Mandela Metropolitan University Port Elizabeth, South Africa -- View this message in context: http://r.789695.n4.nabble.com/Contrasts-with-an-interaction-How-does-one-specify-the-dummy-variables-for-the-interaction-tp3948792p3950225.html Sent from the R help mailing list archive at Nabble.com.
Max Kuhn
2011-Oct-31 14:59 UTC
[R] Contrasts with an interaction. How does one specify the dummy variables for the interaction
This is failing because it is a saturated model and the contrast package tries to do a t-test (instead of a z test). I can add code to do this, but it will take a few days. Max On Fri, Oct 28, 2011 at 2:16 PM, John Sorkin <JSorkin at grecc.umaryland.edu> wrote:> Forgive my resending this post. To data I have received only one response (thank you Bert Gunter), and I still do not have an answer to my question. > Respectfully, > John > > > Windows XP > R 2.12.1 > contrast package. > > > I am trying to understand how to create contrasts for a model that contatains an interaction. I can get contrasts to work for a model without interaction, but not after adding the interaction. Please see code below. The last two contrast statements show the problem. I would appreciate someone letting me know what is wrong with the syntax of my contrast statements. > Thank you, > John > > > library(contrast) > > # Create 2x2 contingency table. > counts=c(50,50,30,70) > row <- ? ?gl(2,2,4) > column <- gl(2,1,4) > mydata <- data.frame(row,column,counts) > print(mydata) > > # Show levels of 2x2 table > levels(mydata$row) > levels(mydata$column) > > > # Models, no interaction, and interaction > fitglm0 <- glm(counts ~ row + column, ? ? ? ? ? ? ?family=poisson(link="log")) > fitglm ?<- glm(counts ~ row + column + row*column, family=poisson(link="log")) > > # Contrasts for model without interaction works fine! > anova(fitglm0) > summary(fitglm0) > con0<-contrast(fitglm0,list(row="1",column="1")) > print(con0,X=TRUE) > > # Contrast for model with interaction does not work. > anova(fitglm) > summary(fitglm) > con<-contrast(fitglm,list(row="1",column="1") > print(con,X=TRUE) > > # Nor does this work. > con<-contrast(fitglm,list(row="1",column="1",row:column=c("0","0"))) > print(con,X=TRUE) > > > > > John David Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > Confidentiality Statement: > This email message, including any attachments, is for ...{{dropped:16}}