[R-sig-ME] Creating a for loop for a mixed-effects model with covariates

@hm@dr215 m@iii@g oii tpg@com@@u @hm@dr215 m@iii@g oii tpg@com@@u
Wed Jul 10 10:44:22 CEST 2019


Hi

 

This is a repeated measures data, and I would like to use a mixed-effects
model to run this. The model includes; independent variables (e.g. ADRB2),
groups (GID), Time, and covariates (e.g. ADRB2_pre) and IDs for the repeated
measures (see below, the dataset). I have 59 variables (independent
variables) with 59 covariates (one for each independent variable). The
covariates are pre_treatment values. 

I am trying to use a "for loop" to run this for 59 variables and covariates
(pre-treatment values). There are two variables that will change in each
model in the loop (see below), how can I make a loop to different these two?


e.g. 

1) ADRB2: if this could be [i]; 2) ADRB2_pre (pre_treatment): is a covariate
(i+1) for variable ADRB2, not sure how this should be looped? 

 

These two variables are listed next to each other (i & i+1) in the
spreadsheet, how I can loop the second one (i+1) as a covariate in the
model? I then need to move to other variables (59 of them) in a loop, and so
on..
Below is a subset of my data, I have 6 groups with 4 subjects within each
group.

Your help would be greatly appreciated. 

I looked at the examples on the web, none of those that I found is similar
to what I intend to do. I was able to make a simple loop for the independent
variable, not for the covariates; and didn't work for 59 variables.

# This is my codes for a simple model

dat <- read.csv("ct_data.csv", header = TRUE, sep=",", na.strings = NA)

# exclude Time 0 form the dataset

dat1 <- subset(dat,   Time >0 , select=1:121) 

 

# factors

dat1$GID <- factor(dat1$GID); dat1$Time <- factor(dat1$Time)

 

library(nlme); 

#Mixed-effects model 

model <- lme(ADRB2 ~ GID + Time + ADRB2_pre, data = dat, random = ~ 1 | ID) 

summary(model)


Repeats

Time

Groups

Independent var_1

Covariate_var1

Independent var_2

Covariate_var2


ID

Time

GID

ADRB2

ADRB2_pre

ACE

ACE_pre


1

0

0

21.58

21.58

21.58

21.58


2

0

0

20.05

20.05

20.05

20.05


3

0

0

21.49

21.49

21.49

21.49


4

0

0

22.08

22.08

22.08

22.08


5

0

1

21.31

21.31

21.31

21.31


6

0

1

20.92

20.92

20.92

20.92


7

0

1

21.21

21.21

21.21

21.21


8

0

1

21.54

21.54

21.54

21.54


9

0

2

20.07

20.07

20.07

20.07


10

0

2

20.66

20.66

20.66

20.66


11

0

2

21.46

21.46

21.46

21.46


12

0

2

21.81

21.81

21.81

21.81


13

0

3

20.08

20.08

20.08

20.08


14

0

3

21.27

21.27

21.27

21.27


15

0

3

21.94

21.94

21.94

21.94


16

0

3

20.8

20.8

20.8

20.8


17

0

4

21.1

21.1

21.1

21.1


18

0

4

20.33

20.33

10.54

10.54


19

0

4

21.55

21.55

21.55

21.55


20

0

4

21.28

21.28

21.28

21.28


21

0

5

20.54

20.54

20.54

20.54


22

0

5

20.69

20.69

20.69

20.69


23

0

5

20.87

20.87

20.87

20.87


24

0

5

22.02

22.02

22.02

22.02


1

1

0

21.56

21.58

21.56

21.58


2

1

0

20.12

20.05

20.12

20.05


3

1

0

21.71

21.49

21.71

21.49


4

1

0

20.77

22.08

20.77

22.08


5

1

1

21.1

21.31

21.1

21.31


6

1

1

21.09

20.92

21.09

20.92


7

1

1

21.05

21.21

21.05

21.21


8

1

1

21.4

21.54

21.4

21.54


9

1

2

19.89

20.07

19.89

20.07


10

1

2

20.93

20.66

20.93

20.66


11

1

2

21.72

21.46

21.72

21.46


12

1

2

21.11

21.81

21.11

21.81


13

1

3

22.1

20.08

22.1

20.08


14

1

3

21.14

21.27

21.14

21.27


15

1

3

20.7

21.94

20.7

21.94


16

1

3

20.98

20.8

20.98

20.8


17

1

4

20.75

21.1

20.75

21.1


18

1

4

20.88

20.33

20.88

10.54


19

1

4

21.19

21.55

21.19

21.55


20

1

4

20.48

21.28

20.48

21.28


21

1

5

20.55

20.54

20.55

20.54


22

1

5

21.77

20.69

21.77

20.69


23

1

5

21.2

20.87

21.2

20.87


24

1

5

20.12

22.02

20.12

22.02


1

2

0

22.45

21.58

22.45

21.58


2

2

0

17.83

20.05

17.83

20.05


3

2

0

21.26

21.49

21.26

21.49


4

2

0

20.69

22.08

20.69

22.08


5

2

1

21.73

21.31

21.73

21.31


6

2

1

22.16

20.92

22.16

20.92


7

2

1

20.85

21.21

20.85

21.21


8

2

1

21.58

21.54

21.58

21.54


9

2

2

22.34

20.07

22.34

20.07


10

2

2

20.91

20.66

20.91

20.66


11

2

2

20.22

21.46

20.22

21.46


12

2

2

21.24

21.81

21.24

21.81


13

2

3

21.18

20.08

21.18

20.08


14

2

3

21.74

21.27

21.74

21.27


15

2

3

20.8

21.94

20.8

21.94


16

2

3

21.71

20.8

11.16

20.8


17

2

4

21.6

21.1

21.6

21.1


18

2

4

19.06

20.33

8.98

10.54


19

2

4

21.26

21.55

21.26

21.55


20

2

4

21.87

21.28

21.87

21.28


21

2

5

20.82

20.54

20.82

20.54


22

2

5

21.1

20.69

21.1

20.69


23

2

5

21.09

20.87

21.09

20.87


24

2

5

20.03

22.02

20.03

22.02

 

 

 


	[[alternative HTML version deleted]]



More information about the R-sig-mixed-models mailing list