0

I keep getting the error below when trying to knit my document. Trying to make a lift chart. It works in RStudio but when trying to knit there is an error. I have gains as the (actual_data, predicted, groups = length(predicted)). What seems to be the issue? Is there any way to resolve this? Able to provide any information needed.

    Error in as.data.frame.default(x) : 
      cannot coerce class '"gains"' to a data.frame

Thank you!

--- output: word_document
knitr::opts_chunk$set(echo = TRUE)
R Markdown
library(readr)
Mod_4_banks <- read_csv("see attached image")

dim(Mod_4_banks)
head(Mod_4_banks, 20)
sapply(Mod_4_banks, mean)
colnames(Mod_4_banks)
sapply(Mod_4_banks, levels)
sum(is.na(Mod_4_banks))
Mod_4_banks <- Mod_4_banks[,-1]
Bank_log_reg <- glm(`Financial Condition` ~., data = Mod_4_banks, family = "binomial" )
options(scipen=999)
summary(Bank_log_reg)
library(gains)
library(caret)
pred_mod <- predict(Bank_log_reg, Mod_4_banks, type = "response")
sample_row <- sample(c(1:20), size = 5)
data.frame(actual = Mod_4_banks$`Financial Condition`[sample_row], predicted = pred_mod[sample_row])
gain.df <- gains(Mod_4_banks$`Financial Condition`, pred_mod, groups = length(pred_mod))

View(gain.df)

plot(c(0,gain.df$cume.pct.of.total*sum(Mod_4_banks$`Financial Condition`))~c(0,gain.df$cume.obs), xlab = "Number of Cases", ylab = "Cummulative", main = "Lift Chart of Financial Condition", type="l")

lines(c(0,sum(Mod_4_banks$`Financial Condition`))~c(0,dim(Mod_4_banks)[1]), lty=2)
```

Short amount of data used for this project

  • Please don't provide data as an image, [how to make great minimal reproducible examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Somewhere in your code is a function that tries to coerce an object of class `gains` to a `data.frame`, my guess is `View(gain.df)` because I'm not sure how this should work within a markdown document – starja Aug 23 '20 at 18:48
  • The [documentation](https://cran.r-project.org/web/packages/gains/gains.pdf) states that `gains` returns an object of class `gains` which is a `list` and that there are methods for `print` and `plot`, so my guess is still `View(gain.df)`, maybe you can try `print(gain.df)` instead – starja Aug 23 '20 at 18:52

0 Answers0