teste = read_csv(here::here("data/experimento-lastfm.csv"),
col_types = cols(.default = col_double(),
user = col_character()))
lastfm = read_csv(here::here("data/experimento-lastfm.csv"),
col_types = cols(.default = col_double(),
user = col_character()))
lastfm = lastfm %>%
sample_n(300) %>%
select(news, old, mediana_pop)
glimpse(lastfm)
## Observations: 300
## Variables: 3
## $ news <dbl> 11, 31, 29, 14, 67, 81, 12, 19, 18, 46, 29, 68, 14, …
## $ old <dbl> 116, 55, 58, 148, 177, 132, 56, 103, 71, 118, 95, 25…
## $ mediana_pop <dbl> 5.928286, 5.691755, 5.134337, 6.040386, 5.657557, 5.…
Utilizaremos ICs para estimar duas métricas sobre os usuários do LastFM em geral durante um perÃodo de 6 meses. Em ambos os casos faremos isso a partir de uma amostra de 300 usuários. As duas métricas são:
funcao_theta = function(df) {
df %>%
pull(news) %>%
mean()
}
theta_c = funcao_theta(lastfm)
set.seed(1212)
amostra = lastfm %>%
sample_n(300)
theta_c = funcao_theta(amostra)
theta_c
## [1] 31.55667
repeticoes = 4000 # pelo menos 2000, mas mais não faz mal.
um_bootstrap <- function(x){
news = x %>% pull(news)
boot_x <- sample(news, # amostre dos dados
size = NROW(x), # tamanho igual ao recebido
replace = TRUE) # aqui é o bootstrap
return(mean(boot_x))
}
# A REAMOSTRAGEM
reamostragens = tibble(i = 1:repeticoes) %>%
mutate(theta_c_s = map_dbl(i, ~ um_bootstrap(amostra)))
reamostragens
intervalo = reamostragens %>%
mutate(erro = theta_c_s - theta_c) %>%
summarise(erro_i = quantile(erro, .05),
erro_s = quantile(erro, .95))
intervalo
intervalo = intervalo %>%
mutate(valor_i = theta_c + erro_i,
valor_s = theta_c + erro_s)
intervalo
confianca = .99
alpha = 1 - confianca
intervalo2 = reamostragens %>%
mutate(erro = theta_c_s - theta_c) %>%
summarise(erro_i = quantile(erro, alpha / 2),
erro_s = quantile(erro, 1 - alpha /2)) %>%
mutate(valor_i = theta_c + erro_i,
valor_s = theta_c + erro_s)
intervalo2
amostra %>%
filter(mediana_pop > 5) %>%
ggplot() +
geom_point(mapping = aes(x = mediana_pop, y = (news/news+old)))
amostra