Sample Function Gives Different Result in Console and in Knitted Document When Seed Is Set

sample function gives different result in console and in knitted document when seed is set

R 3.6.0 changed the sampling method. With the new method (default or Rejection) I get 7. With the old one I get 6:

set.seed(1)
rnorm(1)
#> [1] -0.6264538
sample(1:10, 1)
#> [1] 7

set.seed(1, sample.kind = "Rounding")
#> Warning in set.seed(1, sample.kind = "Rounding"): non-uniform 'Rounding'
#> sampler used
rnorm(1)
#> [1] -0.6264538
sample(1:10, 1)
#> [1] 6

Created on 2019-05-23 by the reprex package (v0.2.1)

So it seems you have somehow set sample.kind = "Rounding" in the console. You can check this from the output of RNGkind().

KnitR HTML output showing incorrect/strange results. Inline code and modifying options not yielding the correct output

When asking a question on Stack Overflow it's essential to provide a minimal reproducible example. In particular, have a good read of the first answer and this advice and this will guide you through the process.

I think we've all struggled to help you (and we want to!) because we can't reproduce your issue. Compare the following R and Rmd code when run or knitted, respectively:

# Generate random exponentials
set.seed(1000)
pop = rexp(1000, 0.2) # lambs is 0.2 with n = 1000
mean(pop)
## [1] 5.015616
var(pop)
## [1] 26.07005

and the Rmd:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
message = TRUE,
warning = TRUE
)
```

```{r}
# Generate random exponentials
set.seed(1000)
pop = rexp(1000, 0.2) # lambs is 0.2 with n = 1000
mean(pop)
var(pop)
```

Which produces the following output:

# Generate random exponentials
set.seed(1000)
pop = rexp(1000, 0.2) # lambs is 0.2 with n = 1000
mean(pop)
## [1] 5.015616
var(pop)
## [1] 26.07005

As you can see, the result are identical from a clean R session and a clean knitr session. This is as expected, because the set.seed(), when set the same, should provide the same results every time (see the set.seed man page). When you change the seed to 357, the results vary together:

              | mean    | var      |
console (`R`) | 4.88... | 22.88... |
knitr (`Rmd`) | 4.88... | 22.88... |

In your second code block your knitr chunk result is correct for the 1000 seed, but the console result of 4.76 is incorrect, suggesting to me your console is producing the incorrect output. This could be for one of a few reasons:

  • You forgot to set the seed in the console before running the rexp() function. If you run this line without setting the seed the result will vary every time. Ensure you run the set.seed(1000) first or use an R script and source this to ensure steps are run through in order.
  • There's something in your global R environment that is affecting your results. This is less likely because you cleared your R environment, but this is one of the reasons it's important to create a new session from time to time, either by closing and re-opening RStudio or pressing CTRL + Shift + F10
  • There might be something set in your RProfile.site or .Rprofile that are setting an option on startup that's affecting your results. Have a look at Customizing startup to open and check your startup options, and if necessary correct them.

The output you're seeing isn't because of scipen because there are no numbers in scientific/engineering notation, and it's not digits because the differences you're seeing are more than differences in rounding.

If these suggestions still don't solve your issue, post the minimal reproducible example and try on other computers.

R markdown Knitting to Pdf issue: r code results are different from the pdf output

The sample function doesn't return the same ind in Rmd and PDF. Both case I used set.seed(4).

From Rmd:

> ind <- sample(seq_len(nrow(Spam)),size = sample_size)
> print(ind)
[1] 2696 42 1351 1276 3741 1197 3329 4163 4359 336 3465 1313 460 4378 1907 2088 4453 2678 4410 3491 3274 4565 2319
[24] 2243 2972 3802 2206 3851 2350 2423 2593 1092 4012 2990 2203 4434 2101 2840 1773 31 4280 1105 2579 825 4122 385
[47] 4105 4061 3294 2572 1769 3394 4074 3680 3722 1915 804 790 4055 3377 2548 324 3875 4147 1023 2852 314 2335 3651
[70] 4405 1554 2861 1858 1566 3735 3123 1451 1997 1185 603 4117 3206 2585 4143 4549 265 207 4464 929 4192 877 572
[93] 2433 1228 1594 379 3449 2002 163 3158 1141 2834 1199 2395 2106 2583 3005 954 4384 3636 1237 1793 1304 2213 3396
[116] 2291 1966 3547 4086 319 640 1761 3836 1184 424 3243 4388 898 2471 1545 1826 841 4472 821 3633 3938 3129 1234
[139] 1929 58 2512 2992 1399 4225 753 1963 2547 4342 3406 1973 1584 4503 2643 4277 1124 3767 946 1235 2130 4591 4002
[162] 4189 1844 1483 1255 321 4535 705 669 3334 4477 2709 890 2229 4510 13 2850 1067 4529 3164 446 2683 939 3953

...

From PDF
Sample Image



Related Topics



Leave a reply



Submit