Hello,
Please I'm still new in R, and don't understand how to fix some of the error messages in the reproducible data.
I need to run a multilevel analysis (on two levels) using Cox frailty survival model on a survey data. My problems are how to write the survey design using the two weights needed and apply them to the analysis.
I have identified the needed variables for the survey design, which are; primary sampling unit (psu) =~v021, level-1 weight=~wt1_1, level-2 weight=~wt2_1 ,strata=~v022.
Please can someone help me with the svydesign code and how to include it and the weights in a model.
I understand I'm supposed to use svycoxph
instead of coxph
in my analysis, but I'm lost as to how to write the survey design using the two level weights I need and actually including them in the frailty model below
library(survey)
#> Warning: package 'survey' was built under R version 4.0.5
#> Loading required package: grid
#> Loading required package: Matrix
#> Loading required package: survival
#> Warning: package 'survival' was built under R version 4.0.5
#>
#> Attaching package: 'survey'
#> The following object is masked from 'package:graphics':
#>
#> dotchart
library(survival)
library(frailtypack)
#> Warning: package 'frailtypack' was built under R version 4.0.5
#> Loading required package: boot
#>
#> Attaching package: 'boot'
#> The following object is masked from 'package:survival':
#>
#> aml
#> Loading required package: MASS
#> Loading required package: survC1
#> Warning: package 'survC1' was built under R version 4.0.5
#> Loading required package: doBy
#> Warning: package 'doBy' was built under R version 4.0.5
#>
#> Attaching package: 'frailtypack'
#> The following object is masked from 'package:survival':
#>
#> cluster
datapasta::df_paste (head(rcom, 100)[, c('pid', 'study_time', 'died', 'v021', 'v022', 'v012', 'wt2_1', 'wt1_1', 'v024', 'v025', 'mat_edu')])
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'head': object 'rcom' not found
data.frame(
pid = c(1,2,3,4,5,6,7,8,9,10,11,12,13,
14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,
61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,
77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,
93,94,95,96,97,98,99,100),
study_time = c(13,9,17,31,39,22,24,0,23,12,9,35,
18,20,60,18,5,46,26,54,37,51,31,55,27,15,39,6,
29,0,9,40,23,12,35,56,14,40,57,42,5,42,39,39,
54,19,52,42,7,28,53,5,28,13,37,0,23,33,27,36,20,
24,58,34,12,44,3,34,14,5,10,40,12,36,19,58,17,
40,39,58,53,53,1,50,2,28,24,13,13,50,46,46,19,6,
32,59,9,30,30,43),
died = c(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0),
v021 = c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,
6),
v022 = c("1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","2","2","2","2","2","2","2",
"2","2","2"),
v012 = c(40,37,27,27,24,32,35,35,34,20,28,
28,26,24,24,25,26,26,26,26,28,27,25,25,27,26,26,
21,21,31,36,36,27,23,32,32,33,33,33,28,25,37,
33,34,33,28,28,29,33,33,33,39,38,38,38,38,24,27,
35,40,22,38,38,21,30,30,30,39,43,18,23,23,25,25,
30,45,26,26,35,35,35,35,32,32,40,25,27,30,30,30,
28,28,18,27,30,30,27,21,21,30),
wt2_1 = c(401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,
401.200012207031,401.200012207031,401.200012207031,401.200012207031,
631.818176269531,631.818176269531,631.818176269531,
631.818176269531,631.818176269531,631.818176269531,631.818176269531,
631.818176269531,631.818176269531,631.818176269531),
wt1_1 = c(2.5074667930603,2.5074667930603,
2.5074667930603,2.5074667930603,2.5074667930603,2.5074667930603,
2.5074667930603,2.5074667930603,2.5074667930603,2.5074667930603,
2.5074667930603,2.5074667930603,2.5074667930603,
2.5074667930603,2.5074667930603,5.1194109916687,5.1194109916687,
5.1194109916687,5.1194109916687,5.1194109916687,5.1194109916687,
5.1194109916687,5.1194109916687,5.1194109916687,
5.1194109916687,5.1194109916687,5.1194109916687,5.1194109916687,
5.1194109916687,5.1194109916687,5.1194109916687,5.1194109916687,
5.1194109916687,5.1194109916687,2.40910983085632,
2.40910983085632,2.40910983085632,2.40910983085632,2.40910983085632,
2.40910983085632,2.40910983085632,2.40910983085632,
2.40910983085632,2.40910983085632,2.40910983085632,2.40910983085632,
2.40910983085632,2.40910983085632,1.06203985214233,
1.06203985214233,1.06203985214233,1.06203985214233,1.06203985214233,
1.06203985214233,1.06203985214233,1.06203985214233,
1.06203985214233,1.06203985214233,1.06203985214233,1.06203985214233,
1.06203985214233,1.06203985214233,1.06203985214233,
1.06203985214233,1.06203985214233,1.06203985214233,1.06203985214233,
2.80098295211792,2.80098295211792,2.80098295211792,
2.80098295211792,2.80098295211792,2.80098295211792,2.80098295211792,
2.80098295211792,2.80098295211792,2.80098295211792,
2.80098295211792,2.80098295211792,2.80098295211792,2.80098295211792,
2.80098295211792,2.80098295211792,2.80098295211792,
2.80098295211792,2.80098295211792,2.80098295211792,2.80098295211792,
2.80098295211792,2.80098295211792,1.24210178852081,
1.24210178852081,1.24210178852081,1.24210178852081,1.24210178852081,
1.24210178852081,1.24210178852081,1.24210178852081,
1.24210178852081,1.24210178852081),
v024 = c("1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1"),
v025 = c("1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","2","2","2","2","2","2","2",
"2","2","2"),
mat_edu = c("5","5","5","4","4","5","4","4","4",
"4","4","4","5","5","5","5","5","5","4","4","5",
"4","4","4","5","3","3","4","4","5","5","5","5","4",
"2","2","0","0","0","5","5","0","1","5","5","3",
"3","5","5","5","5","5","5","5","5","5","5","4","5",
"5","4","5","5","3","4","4","5","3","1","3","3","3",
"1","3","2","1","3","3","4","4","0","0","2","2",
"1","0","4","4","4","4","0","0","3","4","2","2","3",
"3","3","0")
)
#> pid study_time died v021 v022 v012 wt2_1 wt1_1 v024 v025 mat_edu
#> 1 1 13 0 1 1 40 401.2000 2.507467 1 1 5
#> 2 2 9 0 1 1 37 401.2000 2.507467 1 1 5
#> 3 3 17 0 1 1 27 401.2000 2.507467 1 1 5
#> 4 4 31 0 1 1 27 401.2000 2.507467 1 1 4
#> 5 5 39 0 1 1 24 401.2000 2.507467 1 1 4
#> 6 6 22 0 1 1 32 401.2000 2.507467 1 1 5
#> 7 7 24 0 1 1 35 401.2000 2.507467 1 1 4
#> 8 8 0 1 1 1 35 401.2000 2.507467 1 1 4
#> 9 9 23 0 1 1 34 401.2000 2.507467 1 1 4
#> 10 10 12 0 1 1 20 401.2000 2.507467 1 1 4
#> 11 11 9 0 1 1 28 401.2000 2.507467 1 1 4
#> 12 12 35 0 1 1 28 401.2000 2.507467 1 1 4
#> 13 13 18 0 1 1 26 401.2000 2.507467 1 1 5
#> 14 14 20 0 1 1 24 401.2000 2.507467 1 1 5
#> 15 15 60 0 1 1 24 401.2000 2.507467 1 1 5
#> 16 16 18 0 2 1 25 401.2000 5.119411 1 1 5
#> 17 17 5 0 2 1 26 401.2000 5.119411 1 1 5
#> 18 18 46 0 2 1 26 401.2000 5.119411 1 1 5
#> 19 19 26 0 2 1 26 401.2000 5.119411 1 1 4
#> 20 20 54 0 2 1 26 401.2000 5.119411 1 1 4
#> 21 21 37 0 2 1 28 401.2000 5.119411 1 1 5
#> 22 22 51 0 2 1 27 401.2000 5.119411 1 1 4
#> 23 23 31 0 2 1 25 401.2000 5.119411 1 1 4
#> 24 24 55 0 2 1 25 401.2000 5.119411 1 1 4
#> 25 25 27 0 2 1 27 401.2000 5.119411 1 1 5
#> 26 26 15 0 2 1 26 401.2000 5.119411 1 1 3
#> 27 27 39 0 2 1 26 401.2000 5.119411 1 1 3
#> 28 28 6 0 2 1 21 401.2000 5.119411 1 1 4
#> 29 29 29 0 2 1 21 401.2000 5.119411 1 1 4
#> 30 30 0 1 2 1 31 401.2000 5.119411 1 1 5
#> 31 31 9 0 2 1 36 401.2000 5.119411 1 1 5
#> 32 32 40 0 2 1 36 401.2000 5.119411 1 1 5
#> 33 33 23 0 2 1 27 401.2000 5.119411 1 1 5
#> 34 34 12 0 2 1 23 401.2000 5.119411 1 1 4
#> 35 35 35 0 3 1 32 401.2000 2.409110 1 1 2
#> 36 36 56 0 3 1 32 401.2000 2.409110 1 1 2
#> 37 37 14 0 3 1 33 401.2000 2.409110 1 1 0
#> 38 38 40 0 3 1 33 401.2000 2.409110 1 1 0
#> 39 39 57 0 3 1 33 401.2000 2.409110 1 1 0
#> 40 40 42 0 3 1 28 401.2000 2.409110 1 1 5
#> 41 41 5 0 3 1 25 401.2000 2.409110 1 1 5
#> 42 42 42 0 3 1 37 401.2000 2.409110 1 1 0
#> 43 43 39 0 3 1 33 401.2000 2.409110 1 1 1
#> 44 44 39 0 3 1 34 401.2000 2.409110 1 1 5
#> 45 45 54 0 3 1 33 401.2000 2.409110 1 1 5
#> 46 46 19 0 3 1 28 401.2000 2.409110 1 1 3
#> 47 47 52 0 3 1 28 401.2000 2.409110 1 1 3
#> 48 48 42 0 3 1 29 401.2000 2.409110 1 1 5
#> 49 49 7 0 4 1 33 401.2000 1.062040 1 1 5
#> 50 50 28 0 4 1 33 401.2000 1.062040 1 1 5
#> 51 51 53 0 4 1 33 401.2000 1.062040 1 1 5
#> 52 52 5 1 4 1 39 401.2000 1.062040 1 1 5
#> 53 53 28 0 4 1 38 401.2000 1.062040 1 1 5
#> 54 54 13 0 4 1 38 401.2000 1.062040 1 1 5
#> 55 55 37 0 4 1 38 401.2000 1.062040 1 1 5
#> 56 56 0 1 4 1 38 401.2000 1.062040 1 1 5
#> 57 57 23 0 4 1 24 401.2000 1.062040 1 1 5
#> 58 58 33 0 4 1 27 401.2000 1.062040 1 1 4
#> 59 59 27 0 4 1 35 401.2000 1.062040 1 1 5
#> 60 60 36 1 4 1 40 401.2000 1.062040 1 1 5
#> 61 61 20 0 4 1 22 401.2000 1.062040 1 1 4
#> 62 62 24 0 4 1 38 401.2000 1.062040 1 1 5
#> 63 63 58 0 4 1 38 401.2000 1.062040 1 1 5
#> 64 64 34 0 4 1 21 401.2000 1.062040 1 1 3
#> 65 65 12 0 4 1 30 401.2000 1.062040 1 1 4
#> 66 66 44 0 4 1 30 401.2000 1.062040 1 1 4
#> 67 67 3 0 4 1 30 401.2000 1.062040 1 1 5
#> 68 68 34 0 5 1 39 401.2000 2.800983 1 1 3
#> 69 69 14 0 5 1 43 401.2000 2.800983 1 1 1
#> 70 70 5 0 5 1 18 401.2000 2.800983 1 1 3
#> 71 71 10 0 5 1 23 401.2000 2.800983 1 1 3
#> 72 72 40 0 5 1 23 401.2000 2.800983 1 1 3
#> 73 73 12 0 5 1 25 401.2000 2.800983 1 1 1
#> 74 74 36 0 5 1 25 401.2000 2.800983 1 1 3
#> 75 75 19 0 5 1 30 401.2000 2.800983 1 1 2
#> 76 76 58 0 5 1 45 401.2000 2.800983 1 1 1
#> 77 77 17 0 5 1 26 401.2000 2.800983 1 1 3
#> 78 78 40 0 5 1 26 401.2000 2.800983 1 1 3
#> 79 79 39 0 5 1 35 401.2000 2.800983 1 1 4
#> 80 80 58 0 5 1 35 401.2000 2.800983 1 1 4
#> 81 81 53 0 5 1 35 401.2000 2.800983 1 1 0
#> 82 82 53 0 5 1 35 401.2000 2.800983 1 1 0
#> 83 83 1 0 5 1 32 401.2000 2.800983 1 1 2
#> 84 84 50 0 5 1 32 401.2000 2.800983 1 1 2
#> 85 85 2 0 5 1 40 401.2000 2.800983 1 1 1
#> 86 86 28 0 5 1 25 401.2000 2.800983 1 1 0
#> 87 87 24 0 5 1 27 401.2000 2.800983 1 1 4
#> 88 88 13 0 5 1 30 401.2000 2.800983 1 1 4
#> 89 89 13 0 5 1 30 401.2000 2.800983 1 1 4
#> 90 90 50 0 5 1 30 401.2000 2.800983 1 1 4
#> 91 91 46 0 6 2 28 631.8182 1.242102 1 2 0
#> 92 92 46 0 6 2 28 631.8182 1.242102 1 2 0
#> 93 93 19 0 6 2 18 631.8182 1.242102 1 2 3
#> 94 94 6 0 6 2 27 631.8182 1.242102 1 2 4
#> 95 95 32 0 6 2 30 631.8182 1.242102 1 2 2
#> 96 96 59 0 6 2 30 631.8182 1.242102 1 2 2
#> 97 97 9 0 6 2 27 631.8182 1.242102 1 2 3
#> 98 98 30 0 6 2 21 631.8182 1.242102 1 2 3
#> 99 99 30 0 6 2 21 631.8182 1.242102 1 2 3
#> 100 100 43 0 6 2 30 631.8182 1.242102 1 2 0
Frailty <- coxph (Surv(study_time, died) ~ factor(mat_edu) + v025 + frailty(v021,distribution="gamma"), data=rcom)
#> Error in terms.formula(formula, specials = ss, data = data): object 'rcom' not found
Created on 2022-01-18 by the reprex package (v2.0.1)
Thank you for the anticipated help.