Problem with Knitr to PDF

Fairly new R user and learning through edx, so please be patient. I have a dataset that I have split into a training and testing set for machine learning and completed this process as normal with no issues. Even though there are no missing values in the original data when I try and run my code to regularize my model the script returns NAs as output. So I added na.rm = True in my a regularized model, and the model ran correctly.

My problem comes when I try to knit to a PDF. The na.rm = True causes an error and halts the process. If I remove the term it knits correctly but with NAs as the output.

Sorry this is causing pain. What's the text of the error message?

In R, true and false are written in all caps, TRUE or FALSE. That is, your bit of code should read na.rm = TRUE. That may be causing your issue.

If not, could you create a very minimal reprex of your R Markdown doc to demonstrate us what's going on? FAQ: What's a reproducible example (`reprex`) and how do I do one?

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

That is expected, as your custom function RMSE supports only two arguments.

Try something like this:

RMSE <- function(predicted_ratings, true_ratings, ...){
    sqrt(mean((predicted_ratings - true_ratings)^2, ...))
}

To illustrate:

old_RMSE <- function(predicted_ratings, true_ratings){
    sqrt(mean((predicted_ratings - true_ratings)^2))
}

new_RMSE <- function(predicted_ratings, true_ratings, ...){
    sqrt(mean((predicted_ratings - true_ratings)^2, ...))
}

set.seed(seed = 64992)

pred_sim <- sample(c(rnorm(20), rep(NA_real_, 5)))
true_sim <- rnorm(25)

old_RMSE(pred_sim, true_sim)
#> [1] NA

old_RMSE(pred_sim, true_sim, na.rm=T)
#> Error in old_RMSE(pred_sim, true_sim, na.rm = T): unused argument (na.rm = T)

new_RMSE(pred_sim, true_sim)
#> [1] NA

new_RMSE(pred_sim, true_sim, na.rm=T)
#> [1] 1.448873

Created on 2020-05-07 by the reprex package (v0.3.0)

Sorry about this. So, I have attempted to use reprex but because I am running R on a Linux server that isn't mine, I can't get the xclip to install and when I run reprex I get an error saying that X11 requires xclip, and I don't have access to install that.

No input provided and clipboard is not available.
Rendering reprex...
Clipboard on X11 requires 'xclip' (recommended) or 'xsel'.
Unable to put result on the clipboard'

For my original problem:
I have the following libraries installed:

library(tidyverse)
library(caret)
library(data.table)
library(lubridate)
library(knitr)
library(ggplot2)

The code I am running when I get the error is:

RMSE <- function(predicted_ratings, true_ratings){
        sqrt(mean((predicted_ratings - true_ratings)^2))
        }
# Calculated Predicted Ratings with Movie Effect
predicted_ratings <- mu + edx_test %>
        left_join(movie_avgs, by='movieId') %>%
        .$b_i
# Calculate Predicted Ratings with Movie Effect
predicted_ratings <- mu + edx_test %>%
        left_join(movie_avgs, by='movieId') %>% 
        .$b_i
       
# Calculate RMSE for Movie Effect Term Plus Mean (Mu)
RMSE_2 <- RMSE(predicted_ratings, edx_test$rating, na.rm = TRUE)

The error I get from this code is:

Error in RMSE(predicted_ratings, edx_test$rating, na.rm = TRUE) : 
  unused argument (na.rm = TRUE)

@Yarnabrina I cannot thank you enough! I have spent the last two days trying to figure this out and of course, it was something simple. But that's what learning is about.