Reputation: 1
I'm running a generalised least squares (gls) to model BMI trajectories with age by intake of a nutrient. My sample is a cohort of twins, so data are clustered within individuals (twinID; due to repeated height and weight measures over time) and within families (famID; due to closer similarities in BMI between twins than other individuals).
My code currently is:
#model 1
gls_21m_obs <- long_data_m %>%
gls(BMI ~ ns(age,3) * ns(Nutrient,2),
data=.,
correlation = corCAR1(form = ~age|twinID),
na.action = na.omit)
and
#model 2
INT_gls_21m_adj_filt <- long_data_m %>% filter(age>= ageT2_m) %>% #filtering out cases for pre-diary BMI
gls(BMI ~ ns(age,3) * ns(Nutrient,2) + ns(age,3) * Sex + ns(age,3) * CSDS_weight_0m + ns(age,3) * CweeksDelivery
+ ns(age,3) * Cincome + ns(age,3) * MatQual + ns(age,3) * ethnicity + ns(age,3) * Ccigarettes +
ns(age,3) * diabetes + ns(age,3) * CBMI_mother + ns(age,3) * NSSEC3 + ns(age,3) * CIMD +
ns(age,3) * FeedingModality_3 + ns(age,3) * closest_BMI_T2 ,
data=.,
correlation = corCAR1(form = ~age|twinID),
na.action = na.omit)
summary(INT_gls_21m_adj_filt)
I don't know how to add in adjustment for clustering within families. I don't think gls works with the survey package, and adding weights = varIdent(form = ~ 1 | twinID/famID),
as suggested by ChatGPT makes the whole thing stop working.
weights = varIdent(form = ~ 1 | twinID/famID),
<- this made the model stop working (R just took ages to try and run it so I stopped it)
I also tried the following:
#model
gls_21m_obs_Xformula <- long_data_m %>% filter(FormulaFeeders== 0) %>%
gls(BMI ~ ns(age,3) * ns(Nutrient,2),
data=.,
correlation = corCAR1(form = ~age|twinID/famID),
na.action = na.omit)
But the output was identical with the addition of /famID
to without it
Sample data (apologies, it is in long format):
twinID famID Nutrient ageT2_m Sex CSDS_weight_0m CweeksDelivery CSES_composite Cincome MatQual ethnicity Zygosity_new Ccigarettes diabetes CBMI_mother NSSEC3
1 10031 1003 NA NA 1 1.6123552 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
2 10031 1003 NA NA 1 1.6123552 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
3 10031 1003 NA NA 1 1.6123552 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
4 10031 1003 NA NA 1 1.6123552 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
5 10031 1003 NA NA 1 1.6123552 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
6 10031 1003 NA NA 1 1.6123552 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
7 10031 1003 NA NA 1 1.6123552 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
8 10032 1003 NA NA 1 1.7296108 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
9 10032 1003 NA NA 1 1.7296108 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
10 10032 1003 NA NA 1 1.7296108 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
11 10032 1003 NA NA 1 1.7296108 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
12 10032 1003 NA NA 1 1.7296108 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
13 10032 1003 NA NA 1 1.7296108 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
14 10032 1003 NA NA 1 1.7296108 0.7976589 -2.2331242 -4.5410545 1 1 1 -0.7312003 2 0.8570732 0
15 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
16 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
17 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
18 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
19 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
20 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
21 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
22 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
23 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
24 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
25 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
26 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
27 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
28 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
29 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
30 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
31 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
32 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
33 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
34 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
35 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
36 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
37 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
38 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
39 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
40 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
41 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
42 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
43 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
44 10041 1004 17.80472 20.10678 2 -0.1549409 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
45 10042 1004 17.35859 20.10678 2 -2.4764245 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
46 10042 1004 17.35859 20.10678 2 -2.4764245 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
47 10042 1004 17.35859 20.10678 2 -2.4764245 -4.2023411 0.7068758 0.4589455 4 1 1 -0.7312003 2 0.9187060 2
CIMD FeedingModality_3 age BMI closest_BMI_T2
1 5.1551724 2 12.254648 NA NA
2 5.1551724 2 22.373768 17.28843 NA
3 5.1551724 2 25.560634 17.02422 NA
4 5.1551724 2 0.000000 NA NA
5 5.1551724 2 2.595489 NA NA
6 5.1551724 2 6.767983 NA NA
7 5.1551724 2 9.692014 NA NA
8 5.1551724 2 0.000000 NA NA
9 5.1551724 2 2.595489 NA NA
10 5.1551724 2 6.767983 NA NA
11 5.1551724 2 9.692014 NA NA
12 5.1551724 2 12.254648 NA NA
13 5.1551724 2 22.373768 17.43197 NA
14 5.1551724 2 25.560634 16.79286 NA
15 -0.8448276 2 51.876916 14.96599 15.62500
16 -0.8448276 2 35.712608 15.55664 15.62500
17 -0.8448276 2 24.575005 15.67717 15.62500
18 -0.8448276 2 90.809244 15.25128 15.62500
19 -0.8448276 2 14.620157 15.55531 15.62500
20 -0.8448276 2 39.490851 15.51247 15.62500
21 -0.8448276 2 76.780464 15.01189 15.62500
22 -0.8448276 2 55.852285 14.67377 15.62500
23 -0.8448276 2 85.848247 15.28899 15.62500
24 -0.8448276 2 0.000000 NA 15.62500
25 -0.8448276 2 94.718904 15.47313 15.62500
26 -0.8448276 2 72.377990 14.77378 15.62500
27 -0.8448276 2 83.844135 15.05576 15.62500
28 -0.8448276 2 3.055449 NA 15.62500
29 -0.8448276 2 12.254648 NA 15.62500
30 -0.8448276 2 61.536076 14.62810 15.62500
31 -0.8448276 2 9.034928 16.47924 15.62500
32 -0.8448276 2 5.815208 NA 15.62500
33 -0.8448276 2 20.303948 15.62500 15.62500
34 -0.8448276 2 60.911844 14.62810 15.62500
35 -0.8448276 2 80.098747 15.08621 15.62500
36 -0.8448276 2 68.271204 14.98724 15.62500
37 -0.8448276 2 30.061671 15.09479 15.62500
38 -0.8448276 2 33.741351 15.03064 15.62500
39 -0.8448276 2 74.020704 15.15851 15.62500
40 -0.8448276 2 41.166419 15.72962 15.62500
41 -0.8448276 2 48.032965 14.70450 15.62500
42 -0.8448276 2 45.010371 16.06020 15.62500
43 -0.8448276 2 16.919957 NA 15.62500
44 -0.8448276 2 28.024705 15.36674 15.62500
45 -0.8448276 2 35.712608 14.37024 15.02314
46 -0.8448276 2 0.000000 NA 15.02314
47 -0.8448276 2 72.377990 13.87117 15.02314
Upvotes: 0
Views: 74