1

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.

2
  • Rafael, you have a good start to a question, but can you add more info about the vector your function is supposed to produce? And are you getting an error of some sort with your current code? Commented Jan 31, 2013 at 12:27
  • Hopefully now is clearer. Commented Jan 31, 2013 at 13:13

2 Answers 2

2

Your 'estim' dataframe has the wrong dimensions to hold a length-7 vector from the "estimates". You also don't declare "outp", so if the interpreter code ever got there after allowing the dataframe assignment to succeed, that would throw another error. (It's not clear why you take the extra step of assigning to 'outp' anyway.)

Would it be easier to make a 48 row dataframe with columns named c("q12","q21","q23","q32","q14","q24","q34") and just assign to the correct row = i+12*j using sequential indices?

 transRate<-function(period){
  estim<-data.frame(q12=1:48, q21=0, q23=0, q32=0, q14=0, q24=0, q34=0)))

  for(i in seq(1,12)){
    for(j in seq(0,3, by=1)){

    estim[ i+12*j , ] <- qmatrix.msm(cav.msm,  ci="normal", 
              covariates=list( grossTon = i*10, activ=j, period=period))$
                                                  estimates[c(5,2,10,7,13,14,15)]
       }
  }

    return(estim)                     
} 

dataFrame<-transRate(period=1)

(Could not figure out how that 'grosTvect' object with different dimensions was supposed to line up with the "estimates" results.)

Sign up to request clarification or add additional context in comments.

Comments

0

data frame estim has just one column. This column happens to be a matrix. When you do something like estim[i,j], you are accessing the data frame's columns. I believe you actually want to access the matrix inside the data frame. So then you'd have to use something along the lines of estim[1][,c(i,j)] if you want to fill the matrix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.