I fitted a Multistage Markov (MSM) model using 3 covariates. I’m trying to apply a function whose main arguments are fitted MSM model and values of covariates. The function is:
transition<-qmatrix.msm(MSMmod, ci="normal", covariates=list(grossTon= 10, activ=0, period=1))
An output of this function are the estimate values:
Owning<10 Owning10-40 Owning>40 left
Owning<10 -0.18037446 0.06140559 0.00000000 0.118968868
Owning10-40 0.01609661 -0.09027454 0.04502546 0.029152476
Owning>40 0.00000000 0.04801757 -0.05137377 0.003356196
left 0.00000000 0.00000000 0.00000000 0.000000000
and I’m interested just in some of them, so with transition$estimates[c(5,2,10,7,13,14,15)] I’m getting; for example, the values that the function qmatrix.msm gives with the covariates combination grossTon=120, active=0 and period=1 (see below).
[1] 0.06140559 0.01609661 0.04502546 0.04801757 0.118968868 0.029152476 0.003356196
With both for loops in my function I'm expecting get 48 vector (7 values every one,similar to the previous one) as a result of all combinations of grossTon (12 values) and activ(4 values), thus finally to combine all of them into a single vector (336 values, 7*12*4).
This is my function:
transRate<-function(period){
estim<-data.frame(matrix(rep(0,336),336,1))
for(i in seq(10,120,by=10)){
for(j in seq(0,3, by=1)){
estim[c(i,j)]<-qmatrix.msm(msm.Mult4, ci="normal", covariates=list(grossTon=i, activ=j, period=period))$estimates[c(5,2,10,7,13,14,15)]
outp[c(i,j)]<-c(estim[c(i,j)])#Here I'm trying to get my 336 values vector
}
}
grosTvect<-sort(rep(seq(10,120,by=10),28))
rate<-rep(c("q12","q21","q23","q32","q14","q24","q34"),48)
estimRate<-data.frame(grosTvect,rate,outp)
return(estimRate)
}
dataFrame<-transRate(period=1)
I don’t know how to deal with i and j to produce my vector.
I'm getting the following error when I write estim[c(i,j)]
and outp[c(i,j)]<-c(estim[c(i,j)])
error in `[<-.data.frame`(`*tmp*`, c(i, j), value = c(0.0614055886960195, :
new columns would leave holes after existing columns
And this other when I write estim[i,j] and outp<-estim[i,j]
Error in `[<-.data.frame`(`*tmp*`, i, j, value = c(0.0614055886960195, :
replacement has 7 rows, data has 1
Any help will be appreciated.