# Load packages
library(bayesrules)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(fivethirtyeight)
## Some larger datasets need to be installed separately, like senators and
## house_district_forecast. To install these, we recommend you install the
## fivethirtyeightdata package by running:
## install.packages('fivethirtyeightdata', repos =
## 'https://fivethirtyeightdata.github.io/drat/', type = 'source')
##
## Attaching package: 'fivethirtyeight'
##
## The following object is masked from 'package:bayesrules':
##
## bechdel
#a) plot and summarize lambda
# Plot the Gamma(1, 0.25) prior
plot_gamma(shape = 1, rate = 0.25)

# Summaries of gamma
summarize_gamma(shape = 1, rate = 0.25)
## mean mode var sd
## 1 4 0 16 4
#c)data
data("wwc_2019_matches")
wwc_2019_matches <- wwc_2019_matches %>%
mutate(total_goals = score1 + score2)
nrow(wwc_2019_matches)
## [1] 52
#histograma para goles por partido
hist(wwc_2019_matches$total_goals, breaks = 20,xlab = 'goles por partido', ylab = 'Frecuencia', main = 'Distribución de goles por partido')

summary(wwc_2019_matches$total_goals)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000 2.000 3.000 2.808 3.000 13.000
#d)modelo posteriori de lambda dado los datos observados
n=nrow(wwc_2019_matches)
n
## [1] 52
sum_y=sum(wwc_2019_matches$total_goals)
sum_y
## [1] 146
#summarize
summarize_gamma_poisson(shape = 1, rate = 0.25, sum_y = sum_y, n = n)
## model shape rate mean mode var sd
## 1 prior 1 0.25 4.000000 0.000000 16.00000000 4.0000000
## 2 posterior 147 52.25 2.813397 2.794258 0.05384492 0.2320451
#e)plot_poisson_likelihood
plot_poisson_likelihood(y = wwc_2019_matches$total_goals, lambda_upper_bound = 15)

#plot_gamma_poisson
plot_gamma_poisson(shape = 1, rate = 0.25, sum_y = sum_y, n = n,)
