0

I know function durbinWatsonTest in package car use bootstrap method to calculate p-value, then the results are different when i rerun this code. Can someone know how to set seed this function? Many tks.

df <- structure(list(y = c(-2.59, -3.42, -3.19, -3.25, -3.51, -3.18, 
                       -3.36, -3.12, -3.13, -3.02, -2.99, -3.03, -2.9, -2.8, -2.8), 
                 x1 = c(17.25, 9.7, 7.35, 7.93, 8.66, 10.31, 10.32, 11.4, 
                        11.23, 13.13, 13.88, 14, 14, 14, 13), 
                 x2 = c(NA, NA, NA, NA, 17.25, 9.7, 7.35, 7.93, 
                        8.66, 10.31, 10.32, 11.4, 11.23, 13.13, 13.88), 
                 x3 = c(NA, NA, 7.4, 6.5, 6.52, 5.66, 3.1, 
                        3.9, 4.6, 5.4, 5.84, 6.16, 6.52, 6.42, 5.9)), 
            row.names = c(NA, -15L), class = c("tbl_df", "tbl", "data.frame"))

model <- lm(y~x1+x2+x3, data = df)
durbinWatsonTest(model)
Kevin_Nguyen
  • 112
  • 6
  • 1
    I assume that `set.seed()` should do the trick. That influences all within-R random number generation. Do you have a reason to believe that's not working? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Jul 08 '21 at 04:23
  • @MrFlick tks, i try set.seed() but the results are different. – Kevin_Nguyen Jul 08 '21 at 07:45
  • 1
    You need to run `set.seed()` with some value everytime before you call `durbinWatsonTest`, If i put `set.seed(5)` between your `lm` line and `durbinWatsonTest` line, then everytime I re-reun those two lines, I get the same result. The `withr` package can make that a bit easier with `withr::with_seed(5, durbinWatsonTest(model))` – MrFlick Jul 08 '21 at 07:55

0 Answers0