I'm attempting an interval regression in R with censored data containing dependant values either as a number y or an interval [0, z ] containing y .
After searching, I found several sources with examples recommending survival::survreg
(ie here ), though they're not dealing with exactly the same problem. However, I can't get it to work with my data and I assume I'm having some special case.
I'll give you a MWE. First I create some data and the latent intervals:
# data
set.seed(417699)
df <- data.frame(ind = rbinom(10, 1, .75))
df <- transform(df,
value = ifelse(df$ind == 1, sample(1:1000), NA),
value1 = ifelse(df$ind == 0, sample(10:100) * 10, 0),
cv1 = rbinom(10, 2, .7) # 1st independent var.
cv2 = rbinom(10, 2, .25) # 2nd indep. var.
)
# intervals depending if 'ind' equals 0
df$liv <- with(df, ifelse(ind == 1, value, 0))
df$uiv <- with(df, ifelse(ind == 0, value1, value))
df
## ind value value1 cv1 liv uiv cv2
## 1 1 616 1 2 616 616 0
## 2 0 NA 450 2 0 450 0
## 3 1 236 1 2 236 236 0
## 4 1 130 1 1 130 130 1
## 5 0 NA 350 1 0 350 1
## 6 0 NA 250 2 0 250 0
## 7 1 241 1 1 241 241 0
## 8 1 950 1 2 950 950 1
## 9 1 557 1 2 557 557 1
## 10 1 453 1 2 453 453 1
As one can see, there are intervals or points now depending on whether ind = 1 or 0. In detail, if ind = 0, the value lies somewhere in the interval.
Now, with survival::Surv()
and assuming it is left censored I'm creating the "survival object" as follows.
library(survival)
(Y <- with(df, Surv(liv, uiv, event = rep(2, nrow(df)), type = "interval")))
## [1] [837, 837] [ 0, 340] [694, 694] [ 74, 74] [ 0, 280] [ 0, 640] [177, 177]
## [8] [650, 650] [368, 368] [179, 179]
summary(Y)
## time1 time2 status
## Min. : 0.0 Min. : 74.0 Min. :3
## 1st Qu.: 18.5 1st Qu.:204.2 1st Qu.:3
## Median :178.0 Median :354.0 Median :3
## Mean :297.9 Mean :423.9 Mean :3
## 3rd Qu.:579.5 3rd Qu.:647.5 3rd Qu.:3
## Max. :837.0 Max. :837.0 Max. :3
All fine, but at the end survreg()
fails with an error:
survreg(Y ~ cv1 + cv2, data = df, dist = "gaussian")
## Error in coxph.wtest(t(x) %*% (wt * x), c((wt * eta + weights * deriv$dg) %*% :
## NA/NaN/Inf in foreign function call (arg 3)
In Surv()
I tried several values for the options event=
and type=
, most of them didn't work and I'm confused how to specify the right settings (ie I don't know if I'm wrong or the function is, see following note).
Note: survreg()
seems to have had a bug a few versions ago, but which now should be solved (I don't know for sure).
Does anyone know what's going on and how to solve this issue? Moreover, at the moment I guess this seems to be the only promising way to calculate such kind of an interval regression in R, but maybe there is a better option. Thank you.
A tiny comment on this question finally brought me the solution. The trick is to set type = "interval2"
and we can drop the mode=
option.
(Y <- with(df, Surv(liv, uiv, type = "interval2")))
## [1] 616 [ 0, 450] 236 130 [ 0, 350] [ 0, 250] 241
## [8] 950 557 453
summary(Y)
## time1 time2 status
## Min. : 0.0 Min. : 1.0 Min. :1.0
## 1st Qu.: 32.5 1st Qu.: 1.0 1st Qu.:1.0
## Median :238.5 Median : 1.0 Median :1.0
## Mean :318.3 Mean :105.7 Mean :1.6
## 3rd Qu.:531.0 3rd Qu.:187.8 3rd Qu.:2.5
## Max. :950.0 Max. :450.0 Max. :3.0
coef(intreg <- survreg(Y ~ cv1 + cv2, data = df, dist = "gaussian"))
## (Intercept) cv1 cv2
## -282.0126 326.4428 216.9370
Compared to normal OLS the regression results seem to be accurate:
coef(reg <- lm(value ~ cv1 + cv2, data = df))
## (Intercept) cv1 cv2
## -242.5294 364.1176 127.8235
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.