Im bootstraping some samples to calculate slopes (with replacement). Once that is done, the slopes that should have the same distribution, do not have the same distribution. To be clear im not asking for debugging the code, but to understand why im introducing a bias.
Here a reproducible code in R:
N <library(plotly)
N<- 3000
x <- runif(N,0,1)*5
y <- x + rnorm(N, 1, .2)
y2 <- x + rnorm(N, 1, .2)
t.test(y,y2)
dummy <- rep(c(TRUE,FALSE),each = N)
df <- data.frame(x = c(x,x), y = c(y,y2), dummy = dummy)
fig <- plot_ly() %>%
add_trace(x = df[df$dummy == TRUE,]$x, y = df[df$dummy == TRUE,]$y) %>%
add_trace(x = df[df$dummy == FALSE,]$x, y= df[df$dummy == FALSE,]$y)
fig
boot_strap <- function(data, n_bootstraps){
output <- sapply(1:n_bootstraps, function(i){
tmp <- data[sample(seq_len(nrow(data)), nrow(data), replace = TRUE),]
model <- lm(y ~ x, data = tmp)
return(coef(model)[2])})
})
return(output)
}
for (size in c(1e2, 2e2, 5e2, 1e3, 1e4)){
sample_1 <- boot_strap(df[df$dummy == TRUE,], size)
sample_2 <- boot_strap(df[df$$dummy == TRUE,], size)
sample_2 <- boot_strap(df[df$dummy == FALSE,], size)
print(paste0('Size: ', size,' - Pvalue: ', t.test(sample_1, sample_2)$p.value))
#print(paste0('Size: ', size,' - Pvalue: ', ks.test(sample_1, sample_2)$p$p.value))
}
fig <- plot_ly() %>%
add_histogram(sample_1) %>%
add_histogram(sample_2)
fig
The data: Here we do not have a difference as is comming from the same population (as shown by the first T-test) what is expected
but in the other hand, the distribution of the slopes are significantly different.
Where is my bias?