0

在这里,在用户的帮助下,我设法从每个模拟中提取了离散形式的提取时间,例如 1,2,3...,50。但是,由于间隔 20-21 及以上没有任何值,是否有任何编码可以让我在自己内部添加值?因为,如果在该时间间隔内没有读数,这意味着读数在下一个间隔之前是相同的。我使用的编码如下:

library(GillespieSSA) 
parms <- c(beta=0.591,sigma=1/8,gamma=1/7) 
x0 <- c(S=50,E=0,I=1,R=0) 
a <- c("beta*S*I","sigma*E","gamma*I") 
nu <- matrix(c(-1,0,0, 1,-1,0, 0,1,-1, 0,0,1),nrow=4,byrow=TRUE) 
set.seed(12345) 
out <- lapply(X=1:1,FUN=function(x) 
ssa(x0,a,nu,parms,tf=50)$data) 
out a<-as.data.frame(out) 
idx <- diff(ceiling(a$V1)) == 1 a[idx,]
4

1 回答 1

0
## change ==1 to >0
idx <- diff(ceiling(a$V1)) > 0 

## get discrete time series
discrete.data <- a[idx,]

## get the last time step value
end.time <- ceiling(tail(discrete.data$V1,1))

## create an empty data frame with all time steps
new.df <- data.frame(t=0:end.time, S=0, E=0, I=0, R=0)

## replace only those time steps that have valid values
new.df[new.df$t %in% ceiling(discrete.data$V1),2:5] <- discrete.data[,2:5]  

如有必要,缺失值可以替换为NA,取决于您要如何处理它们。

于 2013-05-02T12:40:23.653 回答