R markdown does not find a function

340 views Asked by At

When I run a script in R, there is no error, and everything works.

    library(tweedie)
    data_tweedie <- glm(y ~ ., data = data, family = tweedie(var.power = data_tw$p.max, link.power = 0))

But when I knit the R markdown with the same r section, it gives me an Error

enter image description here

Is it some kind of a bug of the package?

here is a reproducible example

library(tweedie)
x <- seq(0,10,1)
y <- seq(0,20,2)
data <- as.data.frame(y, x)
Model <- glm(y ~ x, data = data, family = tweedie(var.power = 1.5, link.power = 0)) 

When I run it just in console of R studio, there is no problem. But when I include the same exact piece in the R markdown document, it gives an error

1

There are 1 answers

0
ppeloton On

The issue seems to be that the package statmod is not loaded in the R markdown code, which is needed for the tweedie family object (it is not part of tweedie package: https://rdrr.io/cran/statmod/man/tweedie.html)

Adding

library(statmod) 

to the R markdown code, should solve the issue. I am assuming your R script works in the console, because statmod is loaded in the environment either directly or through a dependency. Adding the library solves the issue when I am trying to reproduce it.