For a current project i am trying to calculate the population attributable risk using Hazards obtained from a Cox proportional hazards model. There is a function in the package AF
that does this specifically (link). However, when I try to run the code I get an error that says Error in
[.data.frame(data, , eventvar) : undefined columns selected
, and I have no idea what causes the error.
Some example code:
# Load packages
library(dplyr)
library(magrittr)
library(survival)
library(AF)
# Get data
mydata <- structure(list(id = c(7971001, 3098, 1314, 5178001, 756001, 6787002,
693, 2839001, 1186, 5897002, 6761002, 2839002, 3606001, 4530001,
3094001, 6902001, 489001, 2010, 3451, 4526002, 854001, 1942,
678, 3327, 8381001, 443002, 2920001, 5302001, 6413002, 3645001,
830, 8776001, 7289001, 1198, 3307003, 1159, 5014002, 1727001,
756, 1454, 3198002, 469001, 3823001, 2959001, 3472, 6555002,
3091002, 1047, 2060, 7759001, 906002, 5826002, 6745001, 592001,
3136, 5784001, 1194001, 335001, 2376, 2895, 1627001, 5565002,
1862, 3429, 3425, 5978001, 651, 7833001, 37, 1702, 266, 3282001,
336, 2675001, 804001), exposure = c(1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0,
1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1), event = c(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), time = c(12.7748117727584,
2.08350444900753, 14.8774811772758, 2.06981519507187, 11.0581793292266,
15.4661190965092, 4.90349075975359, 4.67898699520876, 8.4435318275154,
14.1409993155373, 14.1464750171116, 14.4394250513347, 15.6632443531828,
13.2265571526352, 14.839151266256, 9.60164271047228, 11.1567419575633,
14.8692676249144, 14.9322381930185, 5.87268993839836, 14.3928815879535,
14.2012320328542, 10.2724161533196, 13.6317590691307, 13.4401095140315,
12.2929500342231, 5.70841889117043, 14.2368240930869, 14.6858316221766,
15.8083504449008, 14.6255989048597, 15.7015742642026, 8.90349075975359,
15.0609171800137, 4.54483230663929, 1.2703627652293, 9.36892539356605,
10.258726899384, 10.6721423682409, 11.6714579055441, 13.1772758384668,
15.813826146475, 10.8911704312115, 2.51060917180014, 14.5872689938398,
12.5147159479808, 14.1656399726215, 9.18275154004107, 14.2614647501711,
5.8425735797399, 12.2108145106092, 15.9808350444901, 14.3518138261465,
9.29226557152635, 14.1464750171116, 10.113620807666, 7.37850787132101,
9.10061601642711, 14.3326488706366, 11.2689938398357, 13.1060917180014,
4.61875427789186, 8.72005475701574, 14.031485284052, 13.9000684462697,
8.65982203969884, 14.5872689938398, 2.18480492813142, 9.79603011635866,
3.40041067761807, 3.35112936344969, 0.454483230663929, 5.39082819986311,
13.5578370978782, 14.9650924024641)), row.names = c(NA, -75L), class = "data.frame")
# Fit a Cox model
cox_model <- coxph(formula=Surv(time=time, event=event, type="right") ~ 1 + exposure, data=mydata, ties="breslow")
# Calculate PAR
par_model <- AFcoxph(cox_model, data=mydata, exposure ="exposure") # Gives error
par_model <- AFcoxph(cox_model, data=mydata, exposure ="exposure", times="time") # Gives error
par_model <- AFcoxph(cox_model, data=mydata, exposure ="exposure", clusterid="id") # Gives error
par_model <- AFcoxph(cox_model, data=mydata, exposure ="exposure", times="time", clusterid="id") # Gives error
Anyone has an idea what causes this error?