0

I am trying to import data from Stata to R and fit a survival model. I did the following:

library(haven) data <- read_dta("C:/Users/user/Desktop/data.dta") View(data) install.packages(c("survival", "survminer")) library("survival") library("survminer")

It worked well. However, I got errors:

data("data")
Warning message:
In data("data") : data set ‘data’ not found 

fit <- survfit(Surv(data$finaltime, data$GSTATUS_DTHCNS_KI) , data = data)
Error in survfit.Surv(Surv(data$finaltime, data$GSTATUS_DTHCNS_KI), data = data) : 
  the survfit function requires a formula as its first argument

I wonder if you can tell me how to fix this.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
Hatem Ali
  • 1
  • 2
  • 1
    As in your previous thread, please note: 1. Your need for help and your gratitude if you get any are taken as read, and don't need to be spelled out. 2. The spelling is Stata, not STATA. Sorry, I don't use R and have no idea what is wrong. – Nick Cox Aug 02 '21 at 11:45
  • Also, if using R you should add `r` to your tags rather than generic tags like `analysis`. This will help elicit more attention from that community (as opposed to Stata users who happen to know some R) – JR96 Aug 02 '21 at 18:12

1 Answers1

0

The issue is you aren't supplying a formula. As noted in the documentation for survfit one must now supply a formula:

Older releases of the code also allowed the specification for a single curve to omit the right hand of the formula, i.e., survfit(Surv(time, status)), in which case the formula argument is not actually a formula. Handling this case required some non-standard and fairly fragile manipulations, and this case is no longer supported.

Here in an example of a fix, where ~ 1 would be replaced by the formula that fits your research question:

fit <- survfit(Surv(data$finaltime, data$GSTATUS_DTHCNS_KI) ~ 1 , data = data)
summary(fit)

See help("survfit.formula") for more information.

JR96
  • 953
  • 5
  • 12