What is the source of this error & how to rectify it.

I am trying to build a simulator for infectious disease. I pass an index for single infected individual along with the time they got infected. Also the vector of susceptible hosts is given. The infectious period & contact interval are kind of giving which susceptible hosts fall in the network of the infected individual. We pick the minimum contact time to identify the newly infected individuals & then look at the minimum contact times for all susceptibles with all infected at each step. Running this code gives the error : Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 0, 1
I am not sure where this error is being generated from.

i<-1
inf_index<-4
#a<-NULL
susc_index<-c(2,3,1,5)
time_inf<-6
IP<-rexp(length(inf_index),2)
CI<-rexp(length(susc_index),3)
a<-CI<=IP

if(sum(a)==0){
  stop("no epidemic")
} else{
  time<-timemat<-cbind(expand.grid(inf=inf_index,susc=susc_index[a]),infectious_time
                       =time_inf+IP,contact_time=time_inf+CI[a])
  contact_time<-round(time[,"contact_time"],digits=3)
  infectious_time<-time[,"infectious_time"]
  cond<-any(contact_time<=max(infectious_time))}


while(cond & length(susc_index)>0){
  index<-which.min(contact_time)
  infector<-time[index,"inf"]
  time_inf<-time[index,"contact_time"]
  inf_index<-time[index,"susc"]
  susc_index<-susc_index[!susc_index==inf_index]
  print(as.matrix(list(infector=infector,infected=inf_index,time_inf=time_inf)))
  time<-time[!time$susc==inf_index,] #remove newly infected from matrix
  i<-i+1
  IP<-c(IP,rexp(1,2)) #generate ip for newly infected & merge the ip for previous person 
  CI<-rexp(length(susc_index),3) # generate ci with newly infected with susceptible list
  a<-CI<=IP[i]
  
  #contact_time<-round(time[,"contact_time"],3)
  #infectious_time<-time[,"infectious_time"]
  #cat("the while_loop step is now from if =",i,"\n")
    
  timemat<-cbind(expand.grid(inf=inf_index,susc=susc_index[a]),infectious_time
                   =time_inf+IP[i],contact_time=time_inf+CI[a])
  time<-rbind(time,timemat)
  #paste("the time matrix step is now from =",time,"\n")
  contact_time<-round(time[,"contact_time"],3)
  infectious_time<-time[,"infectious_time"]
  cond<-any(contact_time<=max(infectious_time))
}

You only have two data frames, time and time mat, so the error involves one or both.

The error message

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 0, 1

is what you'd expect when you try to tack on a column that has a different number of rows than the other column. And your DFs have different numbers of rows

> nrow(time)
[1] 5
> nrow(timemat)
[1] 3
> 

And, sure enough, the line that throws the error is complaining about the operation on one of them

  timemat<-cbind(expand.grid(inf=inf_index,susc=susc_index[a]),infectious_time
                   =time_inf+IP,contact_time=time_inf+CI[a])

Thanks so is there a way to combine the two data frames without raising this error?
The number of rows for time & timemat will be different in all runs so it will keep giving the error I guess.

Well you can pad with short row, but I did that my immediate reaction would be that I need to think about my ultimate data structure.

From what I've understood of your objective (I'm not an epidemiologist!), the unit of observation is the patient. I'd have one row for every patient. The first column would be the date of the onset of the contagious period for your index patient and blank for everyone else. The second column would be the date of exposure during the contagious period for anyone selected to have contact. The third column is another date column for new exposures to the newly contagious. You continue adding columns.

If you begin with the entire population in column one, you can create your additional columns using dplyr

df <- df %>% mutate(time3 = ifelse(contact, date3, NA)

1 Like

Thanks a lot. Yes my unit of observation are all individuals in a completely susceptible population.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.