Re: [R] maintaining specified factor contrasts when subsetting in lmer
I'm familiar w/ relevel but that doesn't seem to help here (e.g., tried setting the reference level to 3 but the second model w/ the subset argument doesn't seem to estimate the desired model). Possibly another contrasts statement needs to be provided prior to the second model, this time w/ a reduced number of levels, but that seems to introduce problems w.r.t. the total number of levels prior to subsetting. Is the only way around this to define a new dataframe for the subset and define the contrasts for that dataframe accordingly? -Original Message- From: Francisco J. Zagmutt [mailto:[EMAIL PROTECTED] Sent: Friday, July 06, 2007 6:40 PM To: Afshartous, David Cc: r-help@stat.math.ethz.ch Subject: Re: maintaining specified factor contrasts when subsetting in lmer See ?relevel Regards, Francisco Afshartous, David wrote: All, I'm using lmer for some repeated measures data and have specified the contrasts for a time factor such that say time 3 is the base. This works fine. However, when I next use the subset argument to remove the last two time values, the output indicates that the specified contrast is not maintained (see below). I can solve this by creating a new dataframe for the subset of interest and redefining the constrasts, but I was wondering if there is a direct method that allows me to continue w/ the subset argument? (perhaps via supplying a contrast argument to lmer directly, but this doesn't seem possible based on the defintion of this argument in model.matrix.default). Thanks, Dave z - rnorm(24, mean=0, sd=1) time - factor(paste(Time-, rep(1:6, 4), sep=)) Patient - rep(1:4, each = 6) drug - factor(rep(c(D, P), each = 6, times = 2)) ## P = placebo, D = Drug dat.new - data.frame(time, drug, z, Patient) ### specify the contrast as time 3: contrasts(dat.new$time) - contr.treatment(6, base=3) dimnames(contrasts(dat.new$time))[[1]] - as.character(levels(dat.new$time)) dimnames(contrasts(dat.new$time))[[2]] - as.character(levels(dat.new$time)[-3]) fm1 - lmer(z ~ drug + time + (1 | Patient), data = dat.new ) Fixed effects: Estimate Std. Error t value (Intercept) -0.182774 0.464014 -0.39390 drugP -0.281103 0.352309 -0.79789 timeTime-1 0.150505 0.606462 0.24817 timeTime-2 0.612016 0.606462 1.00916 timeTime-4 0.775342 0.606462 1.27847 timeTime-5 0.093741 0.606462 0.15457 timeTime-6 0.452442 0.606462 0.74604 ## time 3 is the base as specified fm2 - lmer(z ~ drug + time + (1 | Patient), data = dat.new, subset = dat.new$time !=Time-6 dat.new$time != Time-5) Fixed effects: Estimate Std. Error t value (Intercept) 0.052975 0.500675 0.10581 drugP -0.451593 0.447818 -1.00843 timeTime-2 0.461511 0.633310 0.72873 timeTime-3 -0.150505 0.633310 -0.23765 timeTime-4 0.624837 0.633310 0.98662 ### time 3 no longer the base; was expecting to see the fixed effects for time-1, time-2, and time-4, w/ Intercept ### representing time-3 [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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] maintaining specified factor contrasts when subsetting in lmer
All, I'm using lmer for some repeated measures data and have specified the contrasts for a time factor such that say time 3 is the base. This works fine. However, when I next use the subset argument to remove the last two time values, the output indicates that the specified contrast is not maintained (see below). I can solve this by creating a new dataframe for the subset of interest and redefining the constrasts, but I was wondering if there is a direct method that allows me to continue w/ the subset argument? (perhaps via supplying a contrast argument to lmer directly, but this doesn't seem possible based on the defintion of this argument in model.matrix.default). Thanks, Dave z - rnorm(24, mean=0, sd=1) time - factor(paste(Time-, rep(1:6, 4), sep=)) Patient - rep(1:4, each = 6) drug - factor(rep(c(D, P), each = 6, times = 2)) ## P = placebo, D = Drug dat.new - data.frame(time, drug, z, Patient) ### specify the contrast as time 3: contrasts(dat.new$time) - contr.treatment(6, base=3) dimnames(contrasts(dat.new$time))[[1]] - as.character(levels(dat.new$time)) dimnames(contrasts(dat.new$time))[[2]] - as.character(levels(dat.new$time)[-3]) fm1 - lmer(z ~ drug + time + (1 | Patient), data = dat.new ) Fixed effects: Estimate Std. Error t value (Intercept) -0.182774 0.464014 -0.39390 drugP -0.281103 0.352309 -0.79789 timeTime-1 0.150505 0.606462 0.24817 timeTime-2 0.612016 0.606462 1.00916 timeTime-4 0.775342 0.606462 1.27847 timeTime-5 0.093741 0.606462 0.15457 timeTime-6 0.452442 0.606462 0.74604 ## time 3 is the base as specified fm2 - lmer(z ~ drug + time + (1 | Patient), data = dat.new, subset = dat.new$time !=Time-6 dat.new$time != Time-5) Fixed effects: Estimate Std. Error t value (Intercept) 0.052975 0.500675 0.10581 drugP -0.451593 0.447818 -1.00843 timeTime-2 0.461511 0.633310 0.72873 timeTime-3 -0.150505 0.633310 -0.23765 timeTime-4 0.624837 0.633310 0.98662 ### time 3 no longer the base; was expecting to see the fixed effects for time-1, time-2, and time-4, w/ Intercept ### representing time-3 [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] maintaining specified factor contrasts when subsetting in lmer
See ?relevel Regards, Francisco Afshartous, David wrote: All, I'm using lmer for some repeated measures data and have specified the contrasts for a time factor such that say time 3 is the base. This works fine. However, when I next use the subset argument to remove the last two time values, the output indicates that the specified contrast is not maintained (see below). I can solve this by creating a new dataframe for the subset of interest and redefining the constrasts, but I was wondering if there is a direct method that allows me to continue w/ the subset argument? (perhaps via supplying a contrast argument to lmer directly, but this doesn't seem possible based on the defintion of this argument in model.matrix.default). Thanks, Dave z - rnorm(24, mean=0, sd=1) time - factor(paste(Time-, rep(1:6, 4), sep=)) Patient - rep(1:4, each = 6) drug - factor(rep(c(D, P), each = 6, times = 2)) ## P = placebo, D = Drug dat.new - data.frame(time, drug, z, Patient) ### specify the contrast as time 3: contrasts(dat.new$time) - contr.treatment(6, base=3) dimnames(contrasts(dat.new$time))[[1]] - as.character(levels(dat.new$time)) dimnames(contrasts(dat.new$time))[[2]] - as.character(levels(dat.new$time)[-3]) fm1 - lmer(z ~ drug + time + (1 | Patient), data = dat.new ) Fixed effects: Estimate Std. Error t value (Intercept) -0.182774 0.464014 -0.39390 drugP -0.281103 0.352309 -0.79789 timeTime-1 0.150505 0.606462 0.24817 timeTime-2 0.612016 0.606462 1.00916 timeTime-4 0.775342 0.606462 1.27847 timeTime-5 0.093741 0.606462 0.15457 timeTime-6 0.452442 0.606462 0.74604 ## time 3 is the base as specified fm2 - lmer(z ~ drug + time + (1 | Patient), data = dat.new, subset = dat.new$time !=Time-6 dat.new$time != Time-5) Fixed effects: Estimate Std. Error t value (Intercept) 0.052975 0.500675 0.10581 drugP -0.451593 0.447818 -1.00843 timeTime-2 0.461511 0.633310 0.72873 timeTime-3 -0.150505 0.633310 -0.23765 timeTime-4 0.624837 0.633310 0.98662 ### time 3 no longer the base; was expecting to see the fixed effects for time-1, time-2, and time-4, w/ Intercept ### representing time-3 [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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.