Context
I'm trying to put a function written by someone else in my own R package but I'm having some problems.
Question
The problem is that the code that calculates std.error gives an error, but when I load the coxme package, the error goes away.
I can't use library(coxme) when writing R package functions. Please tell me how I can successfully calculate std.error.
Reproduciable code
A reproducible example is as follows:
library(survival)
# fit a coxme model
fit <- coxme::coxme(Surv(time, status) ~ ph.ecog + age + (1|inst), lung)
# estimate,nvar and nfrail are for calculating std.error
estimate <- coxme::fixef(fit)
nvar <- base::length(estimate)
nfrail <- base::nrow(fit$var) - nvar
std.error <- base::sqrt(diag(fit$var)[nfrail + 1:nvar])
# Error in as.integer(x) :
# cannot coerce type 'S4' to vector of type 'integer'
When the coxme package is loaded, std.error can be successfully computed.
library(coxme)
std.error <- base::sqrt(diag(fit$var)[nfrail + 1:nvar]) # Running successfully
What I've done
I thought that the
diag()function might come from thecoxmepackage, but after I asked this question at stackoverflow, I found out that thediag()function in thecoxmepackage is from thebasepackage.For the code to calculate
std.errorI refer to this URL: https://stackoverflow.com/a/43720261/11636376