Frailty survival model using survey data

Hello,
I need to run a multilevel analysis (on two levels) using Cox frailty survival model on a survey data. My problem is how to write the design weight using the two weights needed and apply them to the analysis.

I have identified the needed variables for the survey design, which are; 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.
For instance, how do I include them in the frailty model below

Thank you for the anticipated help.

Hi, it would make more sense if you could provide a reproducible example of your dataset.

Hi, thanks for your response.
Here is the reproducible example. Hoping I did the right thing.

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)


rcom1 <- 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),
                  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),
                        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),
                        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),
                        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"),
                        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),
                       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),
                       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),
                        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"),
                        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"),
                     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")
                )

Frailty1 <- coxph (Surv(study_time, died) ~ factor(v024) + factor(mat_edu) + v025 + frailty(v021,distribution="gamma"), data=rcom1)
#> Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]): contrasts can be applied only to factors with 2 or more levels

Created on 2022-01-07 by the reprex package (v2.0.1)

1 Like

Error in unique(x[!is.na(x)]) : object 'psu' not found

What is psu in here:

Model.frailty.gamma <- coxph (Surv(study_time, died) ~ factor(v024) + factor(mat_edu) + v025 + frailty(psu,distribution="gamma"), data=rcom2018)

Cleaner version of your code:

library(survey)
library(survival)

rcom2018 <- data.frame(
  pid = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
  study_time = c(13, 9, 17, 31, 39, 22, 24, 0, 23, 12),
  died = c(0, 0, 0, 0, 0, 0, 0, 1, 0, 0),
  v021 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
  v022 = c("1", "1", "1", "1", "1", "1", "1", "1", "1", "1"),
  wt2_1 = c(401.200012207031,401.200012207031,
            401.200012207031,401.200012207031,401.200012207031,401.200012207031,
            401.200012207031,401.200012207031,401.200012207031,
            401.200012207031),
  wt1_1 = c(2.5074667930603,2.5074667930603,
            2.5074667930603,2.5074667930603,2.5074667930603,2.5074667930603,
            2.5074667930603,2.5074667930603,2.5074667930603,2.5074667930603),
  v024 = c("1", "1", "1", "1", "1", "1", "1", "1", "1", "1"),
  v025 = c("1", "1", "1", "1", "1", "1", "1", "1", "1", "1"),
  mat_edu = c("5", "5", "5", "4", "4", "5", "4", "4", "4", "4")
)

Model.frailty.gamma <- coxph(Surv(study_time, died) ~ factor(v024) + factor(mat_edu) + v025 + frailty(psu, distribution="gamma"), data=rcom2018)

I'm sorry about that. PSU is v021

Please can you see if I have done the right thing in terms of bringing in the needed information.
I've corrected the 'psu' error, and extended the data.

I'm still new in R, and don't understand how to fix some of the error messages there.
My challenges are with writing the survey design (svy) code that recognizes the 2 weights, where psu=~v021, level-1 weight=~wt1_1, level-2 weight=~wt2_1 ,strata=~v022, and including them in the frailty model

Thank you

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.