Skip to content

I am struggling with contrasts

6 messages · Sorkin, John, Berwin A Turlach, Peter Dalgaard +1 more

#
I am running a Poisson regression with a single outcome variable, HGE, and a single independent variable, a factor, Group which can be one of two values, Group1, or Group2.
I am trying to define contrasts that will give me the values of my outcome variable (HGE) when group=Group1 and when group=Group2. After beating my head against a wall day, I have decided to ask for help.

Please see code and below below.

DataForR <-
structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
                                   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
                                   2L), .Label = c("Group1", "Group2"), class = "factor"), HGE = c(3,
                                   1, 0, 0, 0, 1, 0, 0, 0, 0, 3, 5, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 1, 1, 3, 2, 0, 0, 0, 0, 3, 0, 2, 3, 0, 2, 0, 0, 5,
                                   0, 0, 0, 1, 0, 1, 3, 3, 4, 1, 0, 0, 2, 0, 0, 6, 2, 0, 0, 1, 4,
                                   5, 0, 3, 11, 0, 0, 0, 2), logFU = c(3.11397816503905, 2.68510374827232,
                                   0.991192040047273, 0.611801541105993, 1.06925022633126, 2.57001131045749,
                                   0.527354925717201, 0.608027951722353, 1.35812348415319, 1.99809590222588,
                                   2.73814823225949, 2.89037175789616, 1.02290046693241, 0.58120642052779,
                                   1.03160157838515, 0.457336938815005, 1.56760267327335, 2.29561642367795,
                                   -0.296984465114094, 0.4812251539897, 0.606135803570315, -0.349754501094159,
                                   1.34464013381591, 1.03777062941186, 0.604240068405416, 0.416972714959644,
                                   1.90748277192143, 1.92586939817187, 2.84013378690079, 0.611801541105993,
                                   1.57697211118452, 1.09047743846183, 0.412385550952738, -0.129570991408426,
                                   0.752136479035649, 1.3518538711396, 1.44118361285158, 1.24779030782599,
                                   1.0893098960058, 1.40008768325223, 1.36257783450257, 1.06686359035353,
                                   1.93493747038584, 0.549645615615573, -0.0794641713542468, -0.220542769614152,
                                   2.18753270073519, 0.613683009205699, 1.74010061095415, 1.76596671466633,
                                   0.508740117274969, 1.71791855542817, 1.90335053463652, 1.24679280150886,
                                   0.541597282432744, 2.16984703638892, 1.91926871414052, 1.030363188134,
                                   1.07992015565596, 1.78886175639198, 1.07755887947028, -0.117783035656384,
                                   1.49866152280324, 1.03530757397947, 2.68225744654048, 1.58909347171687,
                                   1.49477500411396, 1.47749256296521, 1.77129690199719, 1.5668782980153,
                                   2.06633519417454, 2.62758302063679)), class = "data.frame", row.names = c(NA,
                                   -72L))

# Show that dataForR is a dataframe.
summary(dataForR)
levels(dataForR$Group)
class(dataForR$Group)
# Fit the model.
fit0 <- glm(HGE ~ Group,family=poisson,data=dataForR,offset=logFU)
summary(fit0)

# Get value for Group1
MyContrast=list(xxx=c(1,0))
MyContrast
contrast(fit0,MyContrast)

# Get value for Group2
MyContrast=list(xxx=c(1,1))
MyContrast
contrast(fit0,MyContrast)

I hope you can help me understand how to use the contrast statement.

Thank you,
John




John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
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)
#
G'day John,

On Tue, 10 Mar 2020 01:42:46 +0000
"Sorkin, John" <jsorkin at som.umaryland.edu> wrote:

            
Not sure what you mean, but I am suspecting you are after this output:

R> fit0 <- glm(HGE ~ Group - 1,family=poisson,data=dataForR,offset=logFU)
R> summary(fit0)

Cheers,

	Berwin
#
Yes. Contrasts, by definition, represents between-group differences, so cannot yield individual group levels. The closest you get is that the _intercept_ is the level of the base group in treatment contrasts, and relevel() will allow you to change the base level.

-pd

  
    
#
I have not clearly stated my question. I would like to obtain the point estimate and SE (or point estimate and 95% CI) of a linear combination of the the independent variables included in my regression model. In a simple model having a single categorical variable that has two levels (Group1 and Group2) obtaining the estimate and its SE (or the estimate and a 95% CI) requires knowing the betas produced by the model, the SEs of the betas (which are easily obtained) along with the variance covariance of the estimates. I assume that the variance covariance matrix can be obtained but working the the matrix is a real pain. I am looking for a SIMPLE way to get the point estimate and its SE without having to slog though getting all the estimates, their SEs manually adding them together and including the covariances.
For example if my model is

rate = group and group has the value 1, I want:

beta rate = beta intercept + beta group
variance rate = variance intercept + variance group + 2*covariance (intercept,group)

I suspect I can do this calculation manually, but I would really like to find a way that R will do the computation for me.

My regression model is:
fit1 <- glm(HGE ~ Group,family=quasipoisson(link="log"), data=dataForR,offset=logFU)

In SAS this can be accomplished using estimate statements; I suspect that is an R analogue of the SAS estimate statement, but I don't know that the analogue is .

Thank you,

John

Particular thanks are due to Peter Dalgaard, Berwin Turlach, and Mark Leeds who responded to my original, not well formulated posting.



Thank you,

John





John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
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)
#
Dear John,

The linearHypothesis() function from the 'car' package does this.
Best,
Wolfgang

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Sorkin, John
Sent: Tuesday, 10 March, 2020 11:51
To: peter dalgaard; Berwin A Turlach
Cc: r-help at r-project.org (r-help at r-project.org)
Subject: Re: [R] I am struggling with contrasts

I have not clearly stated my question. I would like to obtain the point estimate and SE (or point estimate and 95% CI) of a linear combination of the the independent variables included in my regression model. In a simple model having a single categorical variable that has two levels (Group1 and Group2) obtaining the estimate and its SE (or the estimate and a 95% CI) requires knowing the betas produced by the model, the SEs of the betas (which are easily obtained) along with the variance covariance of the estimates. I assume that the variance covariance matrix can be obtained but working the the matrix is a real pain. I am looking for a SIMPLE way to get the point estimate and its SE without having to slog though getting all the estimates, their SEs manually adding them together and including the covariances.
For example if my model is

rate = group and group has the value 1, I want:

beta rate = beta intercept + beta group
variance rate = variance intercept + variance group + 2*covariance (intercept,group)

I suspect I can do this calculation manually, but I would really like to find a way that R will do the computation for me.

My regression model is:
fit1 <- glm(HGE ~ Group,family=quasipoisson(link="log"), data=dataForR,offset=logFU)

In SAS this can be accomplished using estimate statements; I suspect that is an R analogue of the SAS estimate statement, but I don't know that the analogue is .

Thank you,

John

Particular thanks are due to Peter Dalgaard, Berwin Turlach, and Mark Leeds who responded to my original, not well formulated posting.

Thank you,

John

John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
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)
#
G'day all,

On Tue, 10 Mar 2020 11:07:13 +0000
"Viechtbauer, Wolfgang (SP)"
<wolfgang.viechtbauer at maastrichtuniversity.nl> wrote:

            
The function glht() in the 'multcomp' package should also be able to do
this.  

The 'emmeans' package might also be useful.

Will be off-line for a while now, but might look at the example again
and how to do it with 'multcomp' or 'emmeans' later.

Cheers,

	Berwin