Referencing the value in the output
I'm new to r, so I'm sorry if this answer is listed elsewhere. I searched as best I could with limited knowledge of what to look for.
I am running a fragility survival analysis simulation using a perfume function and I want to run it multiple times while averaging one of the outputs. I figured out how to reference the values in the outputs in all my other functions (using something like $ coef ["group", "coef"] summary (output)), which doesn't seem to work for this in any way I decided to try. I have listed the result below, the number I am trying to set is 2.076 in the output below (bottom line of output next to "gp2").
>frail1
Frailty distribution: inverse Gaussian
Baseline hazard distribution: Weibull
Loglikelihood: -90.577
ESTIMATE SE p-val
theta 0.043 0.524
rho 1.185 0.228
lambda 0.162 0.046
gp2 2.076 0.423 0
I tried
> names(frail1)
NULL
> coef(frail1)
Error: $ operator is invalid for atomic vectors
> names(summary(frail1))
NULL
> summary(frail1)$coefficients
Error in summary(frail1)$coefficients :
$ operator is invalid for atomic vectors
obviously out of luck. I have listed the attributes below as this is what I have used in the past to find how to reference something. I have also listed a simplified version of the code I am using below. The result is the result.
> attributes(frail1)
$dim
[1] 4 3
$dimnames
$dimnames[[1]]
[1] "theta" "rho" "lambda" "gp2"
$dimnames[[2]]
[1] "ESTIMATE" "SE" "p-val"
$class
[1] "parfm" "matrix"
$call
parfm(formula = Surv(time, event) ~ gp, cluster = "id", data = d1,
dist = "weibull", frailty = "ingau")
$convergence
[1] 0
$it
function
84
$extime
user.self
13.69
$nobs
[1] 100
$shared
[1] FALSE
$loglik
[1] -67.13683
$dist
[1] "weibull"
$dq
[1] 56
$frailty
[1] "ingau"
$clustname
[1] "id"
$correct
[1] 0
$formula
[1] "Surv(time, event) ~ gp"
$terms
[1] "gp"
Simulation code
library(cmprsk)
library(statmod)
library(parfm)
set.seed(0)
shp <- 1 #weibull shape parameter
ns1 <- 50 #group 1 sample size
ns2 <- ns1 #group 2 sample size
hr11 <- 1 #group 1 hazard
hr21 <- 2 #group 2 hazard
frl <- 1 #frailty
alpha1 <- 2
nsim <- 1 #number of simulations
frail <- matrix(1,nsim,1)
id <- seq(1,ns1+ns2)
if(frl==1){
g1et <- matrix(rweibull(ns1,shape=shp,scale=1/(hr11*rinvgauss(1,1,alpha1))),ns1,1)
} else {
g1et <- matrix(rweibull(ns1,shape=shp,scale=1/hr11),ns1,1)
}
g2et <- matrix(rweibull(ns2,shape=shp,scale=1/hr21),ns2,1)
time <- c(g1et,g2et)
gp <- factor(c(matrix(1,ns1,1),matrix(2,ns2,1)),1:2,c(1,2)) #create treatment groups
event <- matrix(round(runif(ns1+ns2,0,1),0),ns1+ns2,1) #select first event type
d1 <- data.frame(id,gp,time,event)
frail1 <- parfm(Surv(time,event)~gp, cluster="id", data=d1, dist="weibull", frailty = "ingau")
frail1
Any help is greatly appreciated!
source to share
First look at frail1 with str
. Instead of being a list, it is actually a matrix
str(frail1)
parfm [1:4, 1:3] 0.043 1.185 0.162 2.076 0.524 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:4] "theta" "rho" "lambda" "gp2"
..$ : chr [1:3] "ESTIMATE" "SE" "p-val"
snipped all the rather long list of attributes
So, you just link to it with [
:
> frail1[4,1]
#[1] 2.076003
source to share