0

I am using Rmarkdown and Kniter in Rstudio to make MS Word document. However, my results are from MSM package. This is my code in the .rmd sript:

knitr::kable(pmatrix.msm(xdata.msm),caption = "Transition probability matrix.")

I am getting this error:

Error in as.data.frame.default(x) : 
  cannot coerce class ""msm.est"" to a data.frame
Calls: <Anonymous> ... format_args -> as.data.frame -> as.data.frame.default

How do I nicely print the MSM model results in Word. Please help me to solve. this.

Update:

Conversion to dataframe gives: Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ""msm.est"" to a data.frame. I need to add caption to pmatrix.msm(xdata.msm).

This is my msm object:

dput(xdata.msm)
structure(c(0.867104394602623, 0.0297597288741373, 0.0111688422294673, 
0.0436200880979502, 0.937379027520689, 0.0642139547902726, 0.0892755172994266, 
0.0328612436051735, 0.92461720298026), .Dim = c(3L, 3L), .Dimnames = list(
    c("State 1", "State 2", "State 3"), c("State 1", "State 2", 
    "State 3")), class = "msm.est")

Update: as.data.frame.matrix() works for some but not for all. please see this object:

dput(pnext.msm(xdata.msm))
structure(list(estimates = structure(c(0, 0.493157008697057, 
0.141450173196988, 0.312417736561033, 0, 0.858549826803012, 0.687582263438967, 
0.506842991302943, 0), .Dim = c(3L, 3L), .Dimnames = list(c("State 1", 
"State 2", "State 3"), c("State 1", "State 2", "State 3"))), 
    L = structure(c(0, 0.404769718769504, 0.0851255671695353, 
    0.229501053449466, 0, 0.775032294188692, 0.599692174664181, 
    0.416275351504334, 0), .Dim = c(3L, 3L)), U = structure(c(0, 
    0.583724648495666, 0.224967705811308, 0.400307825335819, 
    0, 0.914874432830465, 0.770498946550534, 0.595230281230496, 
    0), .Dim = c(3L, 3L))), .Names = c("estimates", "L", "U"), class = "msm.est")

Warning in seq_len(ncols) : first element used of 'length.out' argument
Error in seq_len(ncols) : 
  argument must be coercible to non-negative integer
runjumpfly
  • 319
  • 1
  • 10

2 Answers2

1

Use as.data.frame.matrix(xdata.msm) instead of as.data.frame(xdata.msm). That is, call the matrix method for as.data.frame explicitly. This is because R can't tell that an object of this class is also a matrix, and the package author neglected to provide a data frame coercion method.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
0

I think that your error message is pretty clear. kable deals well with matrices or data frames. Your object is something else and converting it to a data frame doesn't work. You'd have to convert the msm results to a data frame and then you could use kable. Otherwise, simply print out the result of pmatrix.msm(xdata.msm).

epo3
  • 2,991
  • 2
  • 33
  • 60