Re: [R] Syntax for lme function to model random factors and interactions

2012-05-23 Thread i_like_macs
Hi Joshua,

Thank you again for your very-generous help. I think I could follow most of 
your explanations.

To give you some background about myself, I'm doing a PhD at the Institute of 
Sound Recording (IoSR) at the University of Surrey in the UK. As I have no 
statistics background, I've been teaching myself the topic using introductory 
statistics books such as Andy Field's Discovering Statistics using SPSS. We 
do have a statistician on our campus, but I haven't visited him yet because I 
was told that I need to be very specific in the type of questions asked. 
Perhaps it's high time I saw him soon though.

I can clarify what the A, B, C and D factors are. Sorry for not doing 
this before. A and B are components of stimuli used in a listening test. 
C refers to a subject factor, while D refers to a repetition factor (i.e. 
multiple evaluations of the same stimulus by the same subject). I've treated 
A and B as fixed factors because I've assumed their scope extends only 
within the experiment. Regarding C and D, these were treated as random 
factors because the former was assumed to generalise to the general population, 
while the latter was treated as a random factor because a paper which conducted 
a similar listening test did this. Finally, Y is a perceptual attribute with 
numerical ratings.

I've decided not to include the D factor in my model any more because it was 
shown to be not statistically significant in an ANOVA. In this case, is the 
following the correct R code (A and B are fixed factors, C is a random 
factor, 2- and 3-way interactions are modelled and the maximum log-likeliood is 
calculated)?

lme(Y ~ (A + B)^3, data = myData, random = ~ 1 + B | C, method = ML) 

I hope the random = ~ 1 + B | C, syntax is correct here because I'm assuming 
that the subjects do not have independent observations and that the B 
components of the auditory stimuli are rated differently depending on the 
subject. Please let me know if otherwise. Originally I wanted to model 2- and 
3-way interactions for all three factors, However, in the above model, I've 
only specified the interactions between A and B. Are the A:C, B:C and A:B:C 
interactions handled automatically somehow?

Best regards,

Daisuke



--
View this message in context: 
http://r.789695.n4.nabble.com/Syntax-for-lme-function-to-model-random-factors-and-interactions-tp4630744p4631101.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]

__
R-help@r-project.org 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] Syntax for lme function to model random factors and interactions

2012-05-22 Thread Joshua Wiley
See inline

On Mon, May 21, 2012 at 11:17 AM, i_like_macs dk...@mac.com wrote:
 Hello Joshua,

 Many thanks for your help, especially from a fellow Bruin (I went there as
 an undergrad!).

 I understand that there is another forum for mixed models. If my problem
 can't be solved within this thread, I'll have to go there. I do understand
 some theory about mixed models, but obviously am far from an expert.

 My question is not so much statistical advice, as it concerns the correct
 syntax to include random factors and interactions (which include these
 random factors) for the lme function. Maybe it's because I'm used to SPSS,
 but I find R very difficult to use, even after looking up its built-in help.

 I could run your neat code suggestion:

 lme(Y ~ (A + B + C + D)^3, data = myData, random = ~ 1 | C, method = ML)

 but would like to know how to also include D as a random factor. My
 understanding is that the random argument for the lme function is coded
 as:

 ~ x1 + ... + xn | g1 / ... / gm

so in this case x1 ... xn are random effects where the effects are
allowed to vary across levels of g1 .. gm.


 where the left side describes the model for random effects, and the right
 side describes the grouping structure. Reading other posts, I learned that I
 need both sides for the code to run without errors. However, it's not clear

that is correct, the left side is the random effect (random intercepts
and/or random slopes), the right side is whatever variable codes the
levels that the random effect can vary across.


 to me what both sides represent. The left side appears to be where the
 random factors are specified, perhaps like this:

 random = ~ C + D

so this would indicate that C and D are random effects, but you will
need something to indicate what they get to vary across.


 But then this results in errors. Does this mean I have to somehow join the
 two following lines of code to specify both random factors?

 random = ~ 1 | C
 random = ~ 1 | D

I do not think that is what you want (and besides I do not think that
lme() allows multiple random arguments though I could be wrong because
I work with lmer more than lme).


 It's not clear what the ~ 1 represents here, as I would have guessed that
 this is where the random factors would be specified. Is this related to an
 intercept-only model?

yes, ~ 1 | C  means that there is a random intercept for each level of
C.  If you do this, you will get an estimate of the average intercept
for the intercept in your model, but you will also get an estimate of
the variance in intercepts (technically the intercepts are assumed to
come from a normal distribution, you will get the mean and variance
(or standard deviation) of the maximum likelihood estimate of that
distribution).


 I'm sorry for sounding so lost. This is because I am. Perhaps I need to know
 more theory of mixed models, but this seems to be possible only if I

Understanding more theory would probably help.  The Pinheiro and Bates
text as wonderful as it is, may not be the easiest place to start.  I
have not seen you mention anything about a grouping or nesting
structure in your data.  This may be part of the confusion too.  Many
uses of mixed models are for that case.  A classical example would be
students nested within classrooms.  In that case, the research
question could be does number of hours spent on homework predict
grades.  The model could look like:

grades ~ 1 + homework

or in ordinary regression notation instead of R's formula:

grades = Xb + e

where X is a design matrix the first few rows of which might look like:

1  2
1  2.5
1  3
1  2

the first column being the intercept adn the second the number of
hours spent on homework for each student.  b will be a vector of
coefficients, the first coefficient being the estimated intercept and
the second the slope of grades on homework.  e is a vector of
residuals, that part of grades which cannot be explained by the
intercept and homework.  The assumption in ordinary regression is that
e is identically and independently distributed, but students are
within classrooms, and we might guess that in fact, each student was
not really an indepedent observation---there is some similarity
because they share a classroom.  Mixed models address this by adding
random effects.  Following the above example, we might do:

grades ~ 1 + homework
random = ~ 1 | ClassroomID

this allows the intercepts to randomly vary by classroom, which is
sensible---some classes may have more or less skilled students so
given that everyone did 0 hours of homework, we still might expect
some classrooms to have higher or lower grades.  This models that.
Now lets say that further, you think that the effects of homework
might vary across classrooms.  Perhaps for students in very low
performing classes, they get an enormous benefit from spending time on
homework, whereas in the very high performing classes, their grades
only marginally improve for every additional hour of homework. 

[R] Syntax for lme function to model random factors and interactions

2012-05-21 Thread i_like_macs
Hello,

I have a question regarding the syntax of the lme function in the nlme
package. What I'm trying to do is to calculate an estimate of R^2 based on
the likelihood ratio test. For this calculation, I need to determine the
maximum log-likelihood of the intercept-only model and the model of interest
(with the desired factors and interactions).

My model has four independent factors (i.e. A, B, C and D). A and B are
fixed factors, whilst C and D are random factors. 

Presently, I'm trying to code the lme function for the full ANOVA model, but
am unsure how to code both random factors. Additionally, I'm unsure whether
I coded the interactions correctly (i.e. I'm unsure whether the interactions
which contain random factors also need to be specified in the random
argument). I've skimmed through the Pinheiro and Bates book, Mixed-Effects
Models in S and S-PLUS, but could not find an answer.

My R code for the lme function which includes all the main factors and
interactions (2- and 3-way) so far is:

lme(Y ~ A +
B + 
C + 
D + 
A * B + 
A * C + 
A * D + 
B * C + 
B * D + 
C * D + 
A * B * C + 
A * B * D + 
A * C * D + 
B * C * D,
data = myData,
random = ~ 1|C,
method = ML)

Can someone help me with how to code the random factors and interactions
which contain these random factors please? I apologise in advance, that I am
self-taught in statistics, and just started using R. Hence I hope my
question made sense. Thank you very much.

Daisuke Koya
Confused PhD student

--
View this message in context: 
http://r.789695.n4.nabble.com/Syntax-for-lme-function-to-model-random-factors-and-interactions-tp4630744.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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] Syntax for lme function to model random factors and interactions

2012-05-21 Thread Joshua Wiley
Hi,

See inline below

On Mon, May 21, 2012 at 6:03 AM, i_like_macs dk...@mac.com wrote:
 Hello,

 I have a question regarding the syntax of the lme function in the nlme
 package. What I'm trying to do is to calculate an estimate of R^2 based on
 the likelihood ratio test. For this calculation, I need to determine the
 maximum log-likelihood of the intercept-only model and the model of interest
 (with the desired factors and interactions).

 My model has four independent factors (i.e. A, B, C and D). A and B are
 fixed factors, whilst C and D are random factors.

 Presently, I'm trying to code the lme function for the full ANOVA model, but
 am unsure how to code both random factors. Additionally, I'm unsure whether
 I coded the interactions correctly (i.e. I'm unsure whether the interactions
 which contain random factors also need to be specified in the random
 argument). I've skimmed through the Pinheiro and Bates book, Mixed-Effects
 Models in S and S-PLUS, but could not find an answer.

Do they need to be specified for the model to run?  No.  Do they need
to be specified for the model to make sense?  That depends on your
data, theory, and what question you are trying to answer.  I think you
would be better off posting this sort of question to
R-sig-mixedeffects list.  That said, it sounds to me like maybe it
would be easiest to spend some more time learning about mixed effects
models (I know they can be tricky) and then come back to trying to
specify the model in R.  We can help with the code aspect, but do not
typically provide statistical advice on what is or is not an
appropriate model.


 My R code for the lme function which includes all the main factors and
 interactions (2- and 3-way) so far is:

 lme(Y ~ A +
                B +
                C +
                D +
                A * B +
                A * C +
                A * D +
                B * C +
                B * D +
                C * D +
                A * B * C +
                A * B * D +
                A * C * D +
                B * C * D,
                data = myData,
                random = ~ 1|C,
                method = ML)

note that this can be written much more succinctly as:

lme(Y ~ (A + B + C + D)^3, data = myData, random = ~ 1 | C, method = ML)

which will expand to all three-way interactions and lower terms.  Have
you tried running this model?

Cheers,

Josh


 Can someone help me with how to code the random factors and interactions
 which contain these random factors please? I apologise in advance, that I am
 self-taught in statistics, and just started using R. Hence I hope my
 question made sense. Thank you very much.

 Daisuke Koya
 Confused PhD student

 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Syntax-for-lme-function-to-model-random-factors-and-interactions-tp4630744.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org 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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/

__
R-help@r-project.org 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] Syntax for lme function to model random factors and interactions

2012-05-21 Thread i_like_macs
Hello Joshua,

Many thanks for your help, especially from a fellow Bruin (I went there as
an undergrad!). 

I understand that there is another forum for mixed models. If my problem
can't be solved within this thread, I'll have to go there. I do understand
some theory about mixed models, but obviously am far from an expert.

My question is not so much statistical advice, as it concerns the correct
syntax to include random factors and interactions (which include these
random factors) for the lme function. Maybe it's because I'm used to SPSS,
but I find R very difficult to use, even after looking up its built-in help.

I could run your neat code suggestion:

lme(Y ~ (A + B + C + D)^3, data = myData, random = ~ 1 | C, method = ML) 

but would like to know how to also include D as a random factor. My
understanding is that the random argument for the lme function is coded
as:

~ x1 + ... + xn | g1 / ... / gm

where the left side describes the model for random effects, and the right
side describes the grouping structure. Reading other posts, I learned that I
need both sides for the code to run without errors. However, it's not clear
to me what both sides represent. The left side appears to be where the
random factors are specified, perhaps like this:

random = ~ C + D

But then this results in errors. Does this mean I have to somehow join the
two following lines of code to specify both random factors?

random = ~ 1 | C
random = ~ 1 | D

It's not clear what the ~ 1 represents here, as I would have guessed that
this is where the random factors would be specified. Is this related to an
intercept-only model?

I'm sorry for sounding so lost. This is because I am. Perhaps I need to know
more theory of mixed models, but this seems to be possible only if I
understand what parts of the lme function are.

Thank you very much again,

Daisuke


--
View this message in context: 
http://r.789695.n4.nabble.com/Syntax-for-lme-function-to-model-random-factors-and-interactions-tp4630744p4630789.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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] Syntax for lme function to model random factors and interactions

2012-05-21 Thread Peter Ehlers

Inline:

On 2012-05-21 11:17, i_like_macs wrote:

Hello Joshua,

Many thanks for your help, especially from a fellow Bruin (I went there as
an undergrad!).

I understand that there is another forum for mixed models. If my problem
can't be solved within this thread, I'll have to go there. I do understand
some theory about mixed models, but obviously am far from an expert.

My question is not so much statistical advice, as it concerns the correct
syntax to include random factors and interactions (which include these
random factors) for the lme function. Maybe it's because I'm used to SPSS,
but I find R very difficult to use, even after looking up its built-in help.


Just a small comment, since you're interested in correct syntax:
you apparently consider A*B to represent the (A,B)
interaction term; in R, it's A:B and this *is* clearly documented
in the help pages.

Peter Ehlers



I could run your neat code suggestion:

lme(Y ~ (A + B + C + D)^3, data = myData, random = ~ 1 | C, method = ML)

but would like to know how to also include D as a random factor. My
understanding is that the random argument for the lme function is coded
as:

~ x1 + ... + xn | g1 / ... / gm

where the left side describes the model for random effects, and the right
side describes the grouping structure. Reading other posts, I learned that I
need both sides for the code to run without errors. However, it's not clear
to me what both sides represent. The left side appears to be where the
random factors are specified, perhaps like this:

random = ~ C + D

But then this results in errors. Does this mean I have to somehow join the
two following lines of code to specify both random factors?

random = ~ 1 | C
random = ~ 1 | D

It's not clear what the ~ 1 represents here, as I would have guessed that
this is where the random factors would be specified. Is this related to an
intercept-only model?

I'm sorry for sounding so lost. This is because I am. Perhaps I need to know
more theory of mixed models, but this seems to be possible only if I
understand what parts of the lme function are.

Thank you very much again,

Daisuke


--
View this message in context: 
http://r.789695.n4.nabble.com/Syntax-for-lme-function-to-model-random-factors-and-interactions-tp4630744p4630789.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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@r-project.org 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.