I have done a basic search around the site (e.g. R Knit Markdown code chunk: "object not found"), but I don't think I have found anything that fixes my problem, or at least explains why my problem occurs.
There are only a few code chunks in my .rmd which come quite verbatim from https://cran.r-project.org/web/packages/mlogit/vignettes/e1mlogit.html
The first is to load data
rm(list = ls())
library(mlogit)
data("Heating", package = "mlogit")
H <- mlogit.data(Heating, shape = "wide", choice = "depvar", varying = c(3:12))
Then I fit the model, but I use ref_level_var
instead of any specific string "gr"
ref_level_var <- "gr"
mc <- mlogit(formula = depvar ~ ic + oc, data = H, reflevel = ref_level_var)
The odd part comes when I try
head(predict(mc, newdata = H))
Now because I require my files to be knit to two different formats (solution via Knit one markdown file to two output files), I use the following knit
in my header for the .rmd
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile, encoding = encoding, output_format = c("html_document", "pdf_document"))})
However, this results in the following error
Error in model.frame.mFormula(formula = depvar ~ ic + oc, data = structure(list(: object 'ref_level_var' not found
Obviously, it seems that somehow ref_level_var
is not visible to the knitting session, despite there being a ref_level_var <- "gr"
line somewhere. Why does this error occur, and how can I fix it?
Note that the error is very odd, because replacing the whole knit
line with output: html_document
results in no errors.