More loops, simulating data, and plots
FSU
sample() and slice_sample()sample() will take a sample from a vector, and slice_sample() will take a sample from a data framesample() the argument to decide how large of a sample you’ll take is size, and for slice_sample(), it is nreplace to be TRUEUSJudgeRatings, first estimate the minimum, mean, and maximum of each variable
summarize() function from tidyverserbinom(), rnorm(), rnbinom(), rmvnorm()*, etc.pbinom(), pnorm(), pnbinom(), pmvnorm()*, etc.mvtnorm package, while the rest are in base Rrbinom()rbinom()[1] 1 1 0 1 0 1
n is the number of observations, size is the number of trials per observation, and prob is the probability of a successful trial
n or probrbinom() practiceplot or ggplot)rnorm() functionrnorm() takes the arguments mean and sdrnorm()rnorm() to simulate an entire matrix# creating a matrix using the standard normal distribution
mat <- matrix(rnorm(4000), ncol = 4)
head(mat) [,1] [,2] [,3] [,4]
[1,] 1.6609885 0.5292288 -0.2973597 -1.0798235
[2,] 0.7807043 1.2587961 -1.3832009 -2.3535789
[3,] -0.5396074 0.6998001 0.9047782 -1.4692511
[4,] 0.2365920 -1.0059394 -0.3580972 0.9208851
[5,] 0.1890745 -0.7260650 0.3786667 -0.6581734
[6,] 0.7461223 -0.7676132 0.9537298 -0.5252005
# creating a matrix using the standard normal distribution
mat2 <- matrix(c(rnorm(3000),rbinom(1000,1,.5)), ncol = 4)
head(mat2) [,1] [,2] [,3] [,4]
[1,] 0.48793932 0.2840526 -0.1178434 0
[2,] -0.72609351 0.8127428 0.3048668 1
[3,] 0.08315486 1.8190829 1.2636292 0
[4,] 0.21056237 0.4234619 0.3069175 0
[5,] 0.04226511 -2.2886926 1.6162669 0
[6,] 0.03433358 0.1398568 0.7513261 0
coef() and vcov() functions will help here)rmvnorm() function from the mvtnorm package to simulate 1000 draws from the multivariate normal, using the coefficients and variance-covariance matrix from step 1library(tidyverse)
pokemon_data <- read_csv('pokemon_data.csv')
fit <- lm(usage_per ~ type_1+type_2+hp+attack+defense+sp_atk+sp_def+speed+generation+legendary, data = pokemon_data)
as.data.frame(apply(mvtnorm::rmvnorm(10000, mean = coef(fit),sigma = vcov(fit)),
2,quantile, probs = c(.025,.975))) %>%
pivot_longer(cols = 1:ncol(.)) %>%
arrange(name) %>%
mutate(qi = rep(c('lower','upper'), time = nrow(.)/2)) %>%
pivot_wider(names_from = qi, values_from = value) %>%
left_join(as.data.frame(coef(fit)) %>%
mutate(name = rownames(.))) %>%
rename(beta = `coef(fit)`) %>%
select(name,beta,lower,upper) %>%
ggplot(aes(x = beta, y = name))+
geom_pointrange(aes(xmin = lower, xmax = upper))+
geom_vline(aes(xintercept = 0), linetype = 2)+
theme_classic()