I am trying to make variable M5
a time varying covariate but i'm getting the following error. The time variable does not have a zero. I have also tried to give all values less than 1 for the time variable to 1, but I still get the same error.Is there a reason for this error to occur?
library(tidyverse)
#> Registered S3 methods overwritten by 'ggplot2':
#> method from
#> [.quosures rlang
#> c.quosures rlang
#> print.quosures rlang
library(foreign)
library(xlsx)
library(openxlsx)
#>
#> Attaching package: 'openxlsx'
#> The following objects are masked from 'package:xlsx':
#>
#> createWorkbook, loadWorkbook, read.xlsx, saveWorkbook,
#> write.xlsx
library(survival)
library(reprex)
###
########################
####Reading data
setwd("C:\\KDHS 2014\\Data")
kdhs<-read.xlsx("kdhs_new.xlsx",1)
attach(kdhs)
kdhs[1:20,c(1,181,254:257)]
#> ID M5 death monthsDied month time
#> 1 1 NA 0 NA NA 33.00
#> 2 2 NA 0 NA NA 42.00
#> 3 3 52 0 NA NA 12.00
#> 4 4 50 0 NA NA 45.00
#> 5 5 NA 0 NA NA 21.00
#> 6 6 NA 0 NA NA 50.00
#> 7 7 23 0 NA NA 56.00
#> 8 8 8 0 NA NA 15.00
#> 9 9 56 1 0.47 0.47 0.47
#> 10 10 56 1 0.47 0.47 0.47
#> 11 11 NA 0 NA NA 3.00
#> 12 12 NA 0 NA NA 31.00
#> 13 13 NA 0 NA NA 42.00
#> 14 14 11 0 NA NA 43.00
#> 15 15 18 0 NA NA 38.00
#> 16 17 NA 0 NA NA 3.00
#> 17 18 NA 0 NA NA 51.00
#> 18 19 NA 0 NA NA 48.00
#> 19 20 NA 0 NA NA 26.00
#> 20 21 NA 1 0.00 0.02 0.02
#####RENAMING VARIABLES
duration_breastfd<-M5
######### CONVRTING CHARACTOR VARIABLES TO FACTOR VARIABLES
kdhs <- data.frame(kdhs, stringsAsFactors=FALSE)
kdhs<- as.data.frame(unclass(kdhs))
########Time dependent covariates
###length of breastfeeding
time<-ifelse(time<"1",1,time)
newkdhs<- tmerge(data1=kdhs[,-2], data2=kdhs, id=ID, tstop=time)
#> Error in tmerge(data1 = kdhs[, -2], data2 = kdhs, id = ID, tstop = time): found an ending time of 0, the default starting time of 0 is invalid
newkdhs<- tmerge(newkdhs, kdhs, id=ID, brstfd = event(duration_breastfd))
#> Error in tmerge(newkdhs, kdhs, id = ID, brstfd = event(duration_breastfd)): object 'newkdhs' not found
Created on 2019-11-29 by the reprex package (v0.3.0)
type or paste code here