RMarkdown console and Knitted file give different results when using set.seed()

Hi, when I knit a file I get different results from my Markdown console to my knitted file, but only when using a set.seed(). So I'll always get the same results in the console, and always have the same results in the knitted document, but they're different from each other. I've attached pictures of the problem with the code too, in which you can see that the results are different from each other. I also understand there might be an easier way to do it, but I've followed the exact answers from my lectures and this is where I've ended up.

I've copied the results from the console as I can only put one media file in as a new user, and the coefficients have been taken out cause they were formatting weird, but they're also different.

My code

stream <- read_xlsx("mlr.xlsx", "California_streamflow")
glimpse(stream)
fit <- lm(L10BSAAM ~ L10OPRC + L10OBPC, data = stream)

set.seed(100)
stream$random_no <- rnorm(n = nrow(stream), mean = 3, sd = 2)

Creating two regression models

  1. L10BSAAM ~ L10OPRC + L10OBPC
  2. L10BSAAM ~ L10OPRC + L10OBPC + random_no
set.seed(100)
summary(fit)

set.seed(100)
randomno_fit <- lm(L10BSAAM ~ L10OPRC + L10OBPC + random_no, data=stream)

set.seed(100)
summary(randomno_fit)

The result I get in the console

Call:
lm(formula = L10BSAAM ~ L10OPRC + L10OBPC, data = stream)

Residuals:
Min 1Q Median 3Q Max
-0.09832 -0.02350 0.01076 0.03291 0.08568

Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.04937 on 40 degrees of freedom
Multiple R-squared: 0.8749, Adjusted R-squared: 0.8686
F-statistic: 139.8 on 2 and 40 DF, p-value: < 2.2e-16


Call:
lm(formula = L10BSAAM ~ L10OPRC + L10OBPC + random_no, data = stream)

Residuals:
Min 1Q Median 3Q Max
-0.09737 -0.02414 0.01129 0.03284 0.08578

Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.04999 on 39 degrees of freedom
Multiple R-squared: 0.8749, Adjusted R-squared: 0.8653
F-statistic: 90.91 on 3 and 39 DF, p-value: < 2.2e-16


The result I get in the knitted file

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.