R mark down showing values differently when printing as PDF

When i run this code in R studio I get the value [1] 0.008497534 but i knit as PDF the value change to [1] 0.0006579332. Any idea why is this happening please?

(lambda_ridge_min = ridge_cv_fit$lambda.min)

In the chunck header I am using this code: { r, echo=TRUE, warning=FALSE, message=FALSE, results='asis'}

Without seeing your code, it may be that you need to set a seed (set.seed()) for choosing your CV splits. When you knit a document it re-runs in a clean R session and so the splits will change and your results will change.

But sharing part of the code will help diagnose.

I am splitting my data as follow. It is fixed row

train_set = diabetes[1:350, ]
test_set = diabetes[351:442, ]

Not sure how to use ( set.seed()) with in my code. Do I simply place this before subsisting my data. The results change every time I run the code within R studio.

train_set = diabetes[1:350, ]
test_set = diabetes[351:442, ]

Any help would be really appreciated.

Is it possible in R Mark down to say the result then output the result within the text. If that makes sense then i don't have to worry if the value change.

As mentioned by @Yarnabrina, your training/testing splits are fixed, but the CV splits within the training set have a pseudo-random component. Setting a seed will make those random splits "the same" each time you run the code. It won't have any effect on your training and testing splits, since this isn't random, but it will affect the way glmnet chooses the CV folds within the training data.

If I understand you correctly, that is called in-line code, and yes, it is possible, see this simple example.

title: "test"
output: html_document

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
model <- lm(Sepal.Length ~ Sepal.Width, data = iris)
alpha <- model$coefficients[[1]]

## Linear Model
This is the intercept using in-line code `r alpha`


1 Like

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