[R] Getting the same values of adjusted mean and standard errors as SAS

Frank E Harrell Jr feh3k at spamcop.net
Thu May 27 08:44:49 CEST 2004


On Thu, 27 May 2004 16:34:58 +0930
"David J. Netherway" <david.netherway at adelaide.edu.au> wrote:

> Hello,
>  
> I am trying to get the same values for the adjusted means and standard 
> errors using R that are given in SAS for the
> following data. The model is Measurement ~ Age + Gender + Group. I can 
> get the adusted means at the mean age  
> by using predict. I do not know how to get the appropriate standard 
> errors at the adjusted means for Gender
> using values from predict. So I attempted to get them directly from the 
> residuals as follows. The data is at the end
> of the email. While there is a match for the males there is a large 
> difference for the females indicating that what I am doing is wrong.
>  
> #  
> meanAge <- mean(dd$Age)
> meanAgeM <- mean(dd$Age[d$Gender=="M"])
> meanAgeF <- mean(dd$Age[d$Gender=="F"])
. . . .

By using sex-specific means of age you are not getting adjusted estimates
in the usual sense.

I prefer to think of effects as differences in predicted values rather
than as complex SAS-like contrasts. The Design package's contrast function
makes this easy (including SEs and confidence limits):

library(Design)   # also requires Hmisc
d <- datadist(dd); options(datadist='d')
f <- ols(y ~ age + sex + group, data=dd)
contrast(f, list(sex='M'), list(sex='F'))   # usual adjusted difference M
vs F
contrast(f, list(sex='M',age=mean(dd$age[dd$sex=='M']),
            list(sex='F',age=mean(dd$age[dd$sex=='F')) # M vs F not
holding age constant

You can also experiment with specifying age=tapply(age, sex, mean,
na.rm=TRUE) using some of the contrast.Design options.
---
Frank E Harrell Jr   Professor and Chair           School of Medicine
                     Department of Biostatistics   Vanderbilt University




More information about the R-help mailing list