[R] LMER - generate data and define model (2 fixed effects and 1 random effect or 1 fixed effect and 2 random effects)
Rosa Oliveira
rosita21 at gmail.com
Tue Aug 11 11:21:27 CEST 2015
Dear Thierry
id.pat = rep(seq(1:nsample), each =n.longitudinal.observations)
time = rep(seq(1:n.longitudinal.observations)-1, nsample)
age = rnorm(nsample, mean = 36, sd = .8)
f.age = table(cut(age, breaks = 8))
id.age = rep(f.age, each =5)
visit = rnorm(nsample, mean = 2, sd = .03)
f.visit = table(cut(visit, breaks = 3))
id.visit = rep(f.visit, each =5)
1.
I was able to solve the age problem and the number of visits problem. Now the problem is another:
yy~1+id.age+time+(time|id.pat)
or
yy~1+id.age + time+(time|id.pat) + (1|id.visit)
there is no problem, but when I perform LMER, for example:
lmer(yy~1+id.age+time+(time|id.pat)), there are errors:
Error in model.frame.default(drop.unused.levels = TRUE, formula = yy ~ :
variable lengths differ (found for 'time’)
or
Error in model.frame.default(drop.unused.levels = TRUE, formula = yy ~ :
variable lengths differ (found for 'id.age')
2. I wasn’t trying to add the same variable to both random and fixed effects. I’m simulating a model and I’m trying to figure if it makes sense to add some variables into the model.
Another doubt,
3. LMER only accepts categorical variables?
Best,
RO
Atenciosamente,
Rosa Oliveira
--
____________________________________________________________________________
Rosa Celeste dos Santos Oliveira,
E-mail: rosita21 at gmail.com
Tlm: +351 939355143
Linkedin: https://pt.linkedin.com/in/rosacsoliveira
____________________________________________________________________________
"Many admire, few know"
Hippocrates
> On 11 Aug 2015, at 09:00, Thierry Onkelinx <thierry.onkelinx at inbo.be> wrote:
>
> Dear Rosa,
>
> 1) use cut() to convert a continuous variable into a factor. See ?cut for the details.
> 2) The syntax for factors is the same as for continuous variables. Just add the name of the factor variable to the formula
> fAge <- cut(age)
> yy~1+fAge+time+(time|id.pat)
> 3) Add + (1|fAge) to the formula. Note that adding fAge to both the fixed and the random effect doesn't make sense.
> yy~1+time+(time|id.pat) + (1|fAge)
>
> Best regards,
>
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
> Kliniekstraat 25
> 1070 Anderlecht
> Belgium
>
> To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
> The plural of anecdote is not data. ~ Roger Brinner
> The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey
>
> 2015-08-11 1:44 GMT+02:00 Rosa Oliveira <rosita21 at gmail.com <mailto:rosita21 at gmail.com>>:
>
>
> ###############################################################################
>
> # Clear memory and set the working directory and the seed
>
> ###############################################################################
>
> rm(list = ls())
>
> setwd("/Dropbox/LMER/R ")
>
> set.seed(7010)
>
> ###############################################################################
>
> # Load up needed packages and do a require
>
> ###############################################################################
>
> # install.packages("gdata")
>
>
>
> library(nlme)
>
> library(lme4)
>
>
>
> nsample = 1000 # Number of subjects
>
> n.longitudinal.observations = 5 # number of observations per subject
>
>
>
> ###############################################################################
>
> # Set the other parameters
>
> ###############################################################################
>
> id.pat = rep(seq(1:nsample), each =n.longitudinal.observations)
>
> time = rep(seq(1:n.longitudinal.observations)-1, nsample)
>
> age = rnorm(nsample, mean = 36, sd = .8)
> id.age = rep(seq(1: n.longitudinal.observations), each =age)
>
>
>
>
>
> ############################################################################### MODEL WITHOUT AGE
>
> boldBeta_individual_blup = coef(lmer(yy~1+time+(time|id.pat) ))$id.pat #mixed model
>
>
>
>
>
> ############################################################################### MODEL WITH AGE
>
> boldBeta_individual_blup = coef(lmer(yy~1+age+time+(time|id.pat) ))$id.pat #mixed model
>
>
>
> Dear all,
>
>
>
>
>
> I’m trying to use LMER in my simulation problem, and I’m having problems ate the very begging L I’m new in LMER. Can you please help me?
>
>
>
>
>
> 1st problem:
>
> how do I generate age so I can use it as a fixed factor?
>
> 2nd problem:
>
> how do I insert age as a fixed factor?
>
> 3rd problem:
>
> what if I wanted to insert a 2nd random effect based on age?
>
>
>
> Best,
>
> RO
>
>
>
> Atenciosamente,
> Rosa Oliveira
>
> --
> ____________________________________________________________________________
>
>
> Rosa Celeste dos Santos Oliveira,
>
> E-mail: rosita21 at gmail.com <mailto:rosita21 at gmail.com>
> Tlm: +351 939355143 <tel:%2B351%20939355143>
> Linkedin: https://pt.linkedin.com/in/rosacsoliveira <https://pt.linkedin.com/in/rosacsoliveira>
> ____________________________________________________________________________
> "Many admire, few know"
> Hippocrates
>
> ______________________________________________
> R-help at r-project.org <mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help <https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list