0

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?

tcvdb1992
  • 413
  • 3
  • 12

1 Answers1

0

Make sure that your version of R is up-to-date. You can download the latest version here.

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

##snip##

other attached packages:
[1] AF_0.1.5          survival_3.2-13 

There does not seem to be any flaw in your code, since I do not get the errors you are describing.

library(survival)
# install.packages('AF')
library(AF)

cox_model <- coxph(Surv(time, event) ~ exposure, data = mydata, ties="breslow")
par_model <- AFcoxph(cox_model, data = mydata, exposure ="exposure")
par_model_cluster <- AFcoxph(cox_model, data=mydata,
                             exposure ="exposure", clusterid="id")
# ------------------------------------------------------------------
> identical(par_model, par_model_cluster)
[1] TRUE
# ---------------------------------------
> par_model

Estimated attributable fraction (AF) and standard error : 

      Time        AF Std.Error
  4.544832 0.5875401 0.3919930
  4.678987 0.5854788 0.3923111
  8.659822 0.5830906 0.3926216
  9.182752 0.5804960 0.3929152
  9.601643 0.5777211 0.3931865
 10.258727 0.5747394 0.3934271
 12.292950 0.5710542 0.3937199
 12.514716 0.5672505 0.3939801
 13.106092 0.5631900 0.3981882
 13.177276 0.5590750 0.3984770
 13.226557 0.5548138 0.3987241
 14.439425 0.5467442 0.3994595
Dion Groothof
  • 1,406
  • 5
  • 15