Hello all, I am trying to teach myself R and replicate some previous SAS analysis. Could someone please help me translate the following SAS code into R. Proc mixed method=ml Class Group Treatment Stream Time Year; Model Logrpk=Treatment Time Treatment*Time; Random Group Stream (Group Treatment) Year(Time); Thank you to anyone that may help! -- View this message in context: http://r.789695.n4.nabble.com/Conveting-SAS-Proc-mixed-to-R-code-tp3450271p3450271.html Sent from the R help mailing list archive at Nabble.com.
Conveting SAS Proc mixed to R code
5 messages · RKinzer, Kevin Wright, Ben Bolker +1 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110414/d800f8fe/attachment.pl>
Kevin Wright <kw.stat <at> gmail.com> writes:
I am trying to teach myself R and replicate some previous SAS analysis. Could someone please help me translate the following SAS code into R. Proc mixed method=ml Class Group Treatment Stream Time Year; Model Logrpk=Treatment Time Treatment*Time; Random Group Stream (Group Treatment) Year(Time);
Assuming you have a data frame "dat" with these factors: Group Treatment Stream Time Year And continuous response: logrpk This code is a starting point: (I'm not sure exactly what the SAS syntax means). require(lme4) m1 = lmer(logrpk ~ treatment*time + (1|Group) + (1|Stream:Group:Treatment) + (1|Year:Time), data=dat)
Can I please suggest that (Treatment|Stream:Group) or something like it is more appropriate than (1|Stream:Group:Treatment)? In general, what goes on the LEFT of the bar is an intercept or fixed effect (i.e. something that varies between groups); what goes on the RIGHT of the bar is a grouping variable. Thus if a fixed effect terms ends up on the right of the bar, something funny is going on. Ben Bolker
I am desperate for help and thank you to everyone providing input. I am using lme4 for a mixed linear model, and trying to replicate a SAS analysis (see thread below). Variables: Dependent = logrkm; Independent = Group (Streams grouped by similarity), Treatment (3 treatments), Stream, Time (1 or 2; before treatment and after), Year (-8, -7,...7, 8; each yearly observation;negs before treatment and positive after). Design: Blocking by stream group results in unbalanced repeated measures with 3 treatments arranged in blocks with various numbers of observations (Streams) per treatment-block. Stream, Group, and Year are random variables and Treatment and Time are fixed effects. I have tried the following code, but can't seem to replicate the SAS results. Please correct the model below if you see where I am wrong, I am also open to other suggestions. m2<-lmer(logrkm~Treatment*Time+(Treatment|Stream:Group)+(1|Year),data=prototype) Thank you. -- View this message in context: http://r.789695.n4.nabble.com/Conveting-SAS-Proc-mixed-to-R-code-tp3450626p3452937.html Sent from the R help mailing list archive at Nabble.com.
On Fri, Apr 15, 2011 at 8:45 AM, Ben Bolker <bbolker at gmail.com> wrote:
Kevin Wright <kw.stat <at> gmail.com> writes:
I am trying to teach myself R and replicate some previous SAS analysis. Could someone please help me translate the following SAS code into R. Proc mixed method=ml Class Group Treatment Stream Time Year; Model Logrpk=Treatment Time Treatment*Time; Random Group Stream (Group Treatment) Year(Time);
Assuming you have a data frame "dat" with these factors: Group Treatment Stream Time Year And continuous response: logrpk This code is a starting point: (I'm not sure exactly what the SAS syntax means). require(lme4) m1 = lmer(logrpk ~ treatment*time + (1|Group) + (1|Stream:Group:Treatment) + (1|Year:Time), data=dat)
?Can I please suggest that (Treatment|Stream:Group) or something like it is more appropriate than (1|Stream:Group:Treatment)? ?In general, what goes on the LEFT of the bar is an intercept or fixed effect (i.e. something that varies between groups); what goes on the RIGHT of the bar is a grouping variable. ?Thus if a fixed effect terms ends up on the right of the bar, something funny is going on.
I think it is appropriate to have a fixed-effects term on the right hand side in the form of an interaction. I regard both (Treatment|Stream:Group) and (1|Stream:Group:Treatment) as interactions between a fixed-effects factor (Treatment) and a random-effects factor (Stream:Group). The basic rule is that the interaction between a fixed-effects term and a random-effects term is a random effect. It is not appropriate, however, to have Treatment on the right hand side when it is *not* in an interactions. A formula of Response ~ Treatment + (1|Treatment) + ... is nonsensical. Basically the model with (1|Stream) + (1|Stream:Group) + (1|Stream:Group:Treatment) is a restricted form of the model with (1|Stream) + (Treatment|Stream:Group) in which the variance-covariance matrix for random-effects from the last term has the "compound symmetry" form. It is easier to see this if you write the second term as (0+Treatment|Stream:Group).