Another person with knit to pdf problem

Disclaimer: I'm about one week into my R journey

No matter what I do I cannot get knit to pdf to work. The error I get says:

Error in eval(expr, envir, enclos) : object 'thomas_binomial_f' not found
Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval

This is my code:

# Binomial Logistic Regression
# Timeliness is defined as admission within 14 days

# Packages
library(broom)
library(visreg)
library(rcompanion)
library(tidyr)
library(knitr)
library(ggplot2)


# Data
data <- thomas_binomial_f
str(data)

# Model
logistic <- glm(time_ranked ~ cj_ref + gender + prior_admits + drug + freq_ranked + route + first_age_drug, data, family = "binomial")
summary(logistic)

# Fit
nagelkerke(logistic)

# Graphics
visreg(logistic, "drug", scale="response", rug=2, xlab="drug level",
       ylab="P(time_ranked)")

visreg(logistic, "cj_ref", scale="response", rug=2, xlab="cj_ref level",
       ylab="P(time_ranked)")

visreg(logistic, "prior_admits", scale="response", rug=2, xlab="prior_admits level",
       ylab="P(time_ranked)")

visreg(logistic, "first_age_drug", scale="response", rug=2, xlab="first_age_drug level",
       ylab="P(time_ranked)")

visreg(logistic, "drug", scale="response", rug=2, xlab="drug level",
       ylab="P(time_ranked)")

visreg(logistic, "freq_ranked", scale="response", rug=2, xlab="freq_ranked",
       ylab="P(time_ranked)")

# ODDS Ratio
(exp(coef(logistic)))

tidy(logistic, conf.int = FALSE, conf.level = 0.95, exponentiate = FALSE)

I have no idea how bad/good this code is, but obviously something is wrong.
Let me know if there is any other information I can give.
Any help would be sooooo appreciated!

Does it work in RStudio, without knitting? Knitting starts a new session, with a blank environment. Are you specifically loading thomas_binomial_f from somewhere in your code? In any event R can't find it in the knitted session.

Hope this helps,
Anthony

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Yes, it runs well in RSudio so I can’t understand why it doesn’t see thomas_binomial_f when I knit it. I wouldn’t care as much if there was another way to easily export and present what the data and code say.
So with regard to knitting starting a new session, is there a way to find out why it can’t find that object?

I'm kind of surprised there aren't more explicit answers to this problem because there are somany people having a hard time with "object not found" despite it being there in the global environment and linked to the code. I can't figure out how to make it "found" when I go to knit it all. grrrrrrr.

Not getting answers is indeed frustrating, but you already got one.

Note the previous answer by Anthony, which mentions about a new session. Whether the object is present in your current global environment or not has nothing to do with the knitting process. So, in that new session, only objects that you define there, or those are exported from imported libraries are available. If thomas_binomial_f is not present in any of those packages, obviously R fails to run that code with object not found error.

To illustrate, clear all the variables, or just open a new session, in R or RStudio. If you choose RStudio, make sure that you don't load a session as that will defeat the purpose of a fresh session. Now, try to run your code line by line, and not by knitting. It is very likely that you will receive the same error in this line: data <- thomas_binomial_f.

To solve this problem, define thomas_binomial_f inside your notebook, and don't define separately. If you have a CSV or something like that, use read.csv or other alternatives.

Hope this helps.

3 Likes

The way you explained it made a lot of sense and so I actually went back and started over, but this time being very careful to set up the different files the right way, and it worked. The silver lining to all my frustration and research is that I came out with a better understanding of R, my data, and statistics in general. :smiley:

1 Like