ages = rep('adult',2),'juvenile')
snakes
snakes<- list(names = c('Anaconda','Worm Snake','Diamondback'),
kingdom = 'Reptile',
size = c('Large','small','medium'),
legs = 0,
sexes = c('Feale','Male','Female'),
ages = c(rep('adult',2),'juvenile'))
snakes
seals <- list(names = c('Flipper','Dipper','Zipper'),
kingdom = 'Mammal',
size = c('Medium','Large','Large'),
legs = 0,
sexes = c('Male','Male','Female'),
ages = c('juvenile','adult','adult'))
seals
frogs <- list(names = c('Bud','Weis','Er'),
kingdom = 'Amphibian',
size = c('Small','Small','Small'),
legs = 4,
sexes = c('Female','Male','Female'),
ages = c('adult','adult','adult'))
kangaroos <- list(names = c('Ausie','Zipper','Roo'),
kingdom = 'Amphibian',
size = c('Large','Large','Medium'),
legs = 2,
sexes = c('Female','Male','Female'),
ages = c('adult','adult','Juvenile'))
zoo<-list(snakes,giraffes,seals,frogs,kangaroos)
zoo
#Lists are a lot like sets
giraffes <- list(names = c('Molly','Isaac','Bobby'),
kingdom = 'Mammal',
size = 'Large',
legs = 4,
sexes = c('Female','Male','Male'),
ages = rep('adult',3),
herbivorous = T)
snakes<- list(names = c('Anaconda','Worm Snake','Diamondback'),
kingdom = 'Reptile',
size = c('Large','small','medium'),
legs = 0,
sexes = c('Feale','Male','Female'),
ages = c(rep('adult',2),'juvenile'),
herbivorous = T)
seals <- list(names = c('Flipper','Dipper','Zipper'),
kingdom = 'Mammal',
size = c('Medium','Large','Large'),
legs = 0,
sexes = c('Male','Male','Female'),
ages = c('juvenile','adult','adult'),
herbivorous = F)
frogs <- list(names = c('Bud','Weis','Er'),
kingdom = 'Amphibian',
size = c('Small','Small','Small'),
legs = 4,
sexes = c('Female','Male','Female'),
ages = c('adult','adult','adult'),
herbivorous = F)
kangaroos <- list(names = c('Ausie','Zipper','Roo'),
kingdom = 'Amphibian',
size = c('Large','Large','Medium'),
legs = 2,
sexes = c('Female','Male','Female'),
ages = c('adult','adult','Juvenile'),
herbivorous = T)
zoo<-list(snakes,giraffes,seals,frogs,kangaroos)
zoo
#Sometimes we want packages to be as up-to-date as possible...
#other times we want to ensure the script uses the same version every time
groundhog::groundhog.library()
install.packages("groundhog")
pkgs <- c("rio","metafor")
groundhog::groundhog.library(pkgs, "2023-09-01")
groundhog::groundhog.library(pkgs,
"2023-09-01",
tolerate.R.version = '4.5.0')
#----Functions!----
f=expression(x^2+3*x)
D(f)
D(f,'x')
#Take the derivative
df<-D(f,'x')
#Take the derivative
(df<-D(f,'x'))
#You can also integrate
integrate(f,0,1)
#----Functions!----
#Define a function
f=expression(x^2+3*x)
#You can also integrate
integrate(f,0,1)
#----Functions!----
#Define a function
f=function(x)x^2+3*x
#Take the derivative
(df<-D(f,'x'))
#You can also integrate
g<- function(x) 3x+exp(x)
#You can also integrate
g <- function(x) x*3+exp(x)
g
integrate(g,0,1)
integrate(g,
lower = 0,
upper = 1)
x <- seq(-6, 3, length.out = 100)
# Evaluate the function
y <- eval(f)
y
#----Functions!----
#Define a function
f=function(x) x^2+3*x
#Take the derivative
(df<-D(f,'x'))
#Let's plot it!
x <- seq(-6, 3, length.out = 100)
# Evaluate the function
y <- eval(f)
# Create a data frame
df <- data.frame(x = x, y = y)
x
# Evaluate the function
y <- eval(x)
y
# Create a data frame
df <- data.frame(x = x, y = y)
#Let's plot it!
x <- seq(-6, 3, length.out = 100)
# Evaluate the function
y <- eval(f)
# Create a data frame
df <- data.frame(x = x, y = y)
# Evaluate the function
y <- f(x)
y
# Create a data frame
df <- data.frame(x = x, y = y)
# Create the ggplot
ggplot(df, aes(x = x, y = y)) +
geom_line(color = "blue", size = 1.2) +
geom_hline(yintercept = 0, color = "gray", linetype = "dashed", alpha = 0.7) +
geom_vline(xintercept = 0, color = "gray", linetype = "dashed", alpha = 0.7) +
labs(
title = "Plot of f(x) = x² + 3x",
x = "x",
y = "f(x)"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 14),
panel.grid.major = element_line(alpha = 0.3),
panel.grid.minor = element_line(alpha = 0.2)
)
ggplot(df, aes(x = x, y = y)) +
geom_line(color = "blue", size = 1.2) +
geom_hline(yintercept = 0, color = "gray", linetype = "dashed", alpha = 0.7) +
geom_vline(xintercept = 0, color = "gray", linetype = "dashed", alpha = 0.7) +
labs(
title = "Plot of f(x) = x² + 3x",
x = "x",
y = "f(x)"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 14),
panel.grid.major = element_line(alpha = 0.3),
panel.grid.minor = element_line(alpha = 0.2)
)
library(tidyverse)
ggplot(df, aes(x = x, y = y)) +
geom_line(color = "blue", size = 1.2) +
geom_hline(yintercept = 0, color = "gray", linetype = "dashed", alpha = 0.7) +
geom_vline(xintercept = 0, color = "gray", linetype = "dashed", alpha = 0.7) +
labs(
title = "Plot of f(x) = x² + 3x",
x = "x",
y = "f(x)"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 14),
panel.grid.major = element_line(alpha = 0.3),
panel.grid.minor = element_line(alpha = 0.2)
)
#Exercise: get the data in a workable form and plot it.
ggplot(df, aes(x = x, y = y)) +
geom_line(color = "blue", size = 1.2) +
geom_hline(yintercept = 0, color = "gray", linetype = "dashed", alpha = 0.7) +
geom_vline(xintercept = 0, color = "gray", linetype = "dashed", alpha = 0.7) +
labs(
title = "Plot of f(x) = x² + 3x",
x = "x",
y = "f(x)"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 14),
panel.grid.major = element_line(alpha = 0.3),
panel.grid.minor = element_line(alpha = 0.2)
)
#Exercise: get the data in a workable form and plot it.
ggplot(df, aes(x = x, y = y)) +
geom_line(color = "blue", size = 1.2) +
geom_hline(yintercept = 0, color = "gray", linetype = "dashed", alpha = 0.7) +
geom_vline(xintercept = 0, color = "gray", linetype = "dashed", alpha = 0.7) +
labs(
title = "Plot of f(x) = x² + 3x",
x = "x",
y = "f(x)"
) +
theme_minimal()
giraffes <- list(names = c('Molly','Isaac','Bobby'),
kingdom = 'Mammal',
size = 'Large',
legs = 4,
sexes = c('Female','Male','Male'),
ages = rep('adult',3),
herbivorous = T)
snakes<- list(names = c('Anaconda','Worm Snake','Diamondback'),
kingdom = 'Reptile',
size = c('Large','small','medium'),
legs = 0,
sexes = c('Feale','Male','Female'),
ages = c(rep('adult',2),'juvenile'),
herbivorous = T)
seals <- list(names = c('Flipper','Dipper','Zipper'),
kingdom = 'Mammal',
size = c('Medium','Large','Large'),
legs = 0,
sexes = c('Male','Male','Female'),
ages = c('juvenile','adult','adult'),
herbivorous = F)
frogs <- list(names = c('Bud','Weis','Er'),
kingdom = 'Amphibian',
size = c('Small','Small','Small'),
legs = 4,
sexes = c('Female','Male','Female'),
ages = c('adult','adult','adult'),
herbivorous = F)
kangaroos <- list(names = c('Ausie','Zipper','Roo'),
kingdom = 'Amphibian',
size = c('Large','Large','Medium'),
legs = 2,
sexes = c('Female','Male','Female'),
ages = c('adult','adult','Juvenile'),
herbivorous = T)
zoo<-list(snakes,giraffes,seals,frogs,kangaroos)
zoo
#let's throw darts at a 1x1 dartboard
darts<-data.frame(x = runif(100000,-.5,.5),
y = runif(100000,-.5,.5))
darts
#how many fall within a circle with formula x^2 + y^2 = 0.5^2
darts$circle<-ifelse(darts$x^2 + darts$y^2 > .5^2,
0,
1)
darts
#how many fall within a circle with formula x^2 + y^2 = 0.5^2
darts$circle<-ifelse(darts$x^2 + darts$y^2 > .5^2,
0,
1)
#plotting it
ggplot(darts,
aes(x = x,
y = y,
color= as.factor(circle)))+
geom_point()
#Purpose: Consider randomness and practice simulations
#Clear environment
rm(list = ls())
library(tidyverse)
#let's throw darts at a 1x1 dartboard
darts<-data.frame(x = runif(100000,-.5,.5),
y = runif(100000,-.5,.5))
#how many fall within a circle with formula x^2 + y^2 = 0.5^2
darts$circle<-ifelse(darts$x^2 + darts$y^2 > .5^2,
0,
1)
#plotting it
ggplot(darts,
aes(x = x,
y = y,
color= as.factor(circle)))+
geom_point()
#compare to pi * r^2
mean(darts$circle)
pi*.5^2
#Purpose: What do these concepts look like in code?
#Clear environment
rm(list = ls())
library(tidyverse)
library(roxygen2)
library(plotly)
library(rgl)
set.seed(60637)
#What are asymptotics?
#In probability theory, asymptotic analysis is the study of limiting behavior.
#By limiting behavior, we mean the behavior of some random process as the number of observations gets larger and larger.
func<-function(x) 1/x
x<-seq(-1e3, 1e3, length.out = 1e4)
x
y<-func(x)
y
ggplot(aes(x = x, y = y))+
geom_point()
ggplot()+
geom_point(aes(x = x, y = y))
x<-seq(-1e2, 1e2, length.out = 1e4)
y<-func(x)
ggplot()+
geom_point(aes(x = x, y = y))
x<-seq(-1, 1, length.out = 1e4)
y<-func(x)
ggplot()+
geom_point(aes(x = x, y = y))
x<-seq(-.1, .1, length.out = 1e4)
y<-func(x)
ggplot()+
geom_point(aes(x = x, y = y))
x<-seq(-1e-10, 1e-10, length.out = 1e4)
y<-func(x)
ggplot()+
geom_point(aes(x = x, y = y))
x<-seq(-1e-10, 1e-10, length.out = 1e4)
y<-func(x)
ggplot()+
geom_point(aes(x = x, y = y))
x<-seq(-1, 1, length.out = 1e4)
y<-func(x)
ggplot()+
geom_point(aes(x = x, y = y))
ggplot()+
geom_point(aes(x = x, y = y)) +
scale_y_continuous(limits = c(-100,100))
ggplot()+
geom_line(aes(x = x, y = y)) +
scale_y_continuous(limits = c(-100,100))
sample(c('H','T'),
size = 1)
x<-seq(0, 1, length.out = 1e4)
y<-func(x)
ggplot()+
geom_line(aes(x = x, y = y)) +
scale_y_continuous(limits = c(-100,100))
ggplot()+
geom_line(aes(x = x, y = y)) +
scale_y_continuous(limits = c(-100,100)) +
geom_hline(yintercept = 0)
x<-seq(0, 100, length.out = 1e4)
y<-func(x)
ggplot()+
geom_line(aes(x = x, y = y)) +
scale_y_continuous(limits = c(-100,100)) +
geom_hline(yintercept = 0)
x<-seq(0, 10, length.out = 1e4)
y<-func(x)
ggplot()+
geom_line(aes(x = x, y = y)) +
scale_y_continuous(limits = c(-100,100)) +
geom_hline(yintercept = 0)
x<-seq(0, 5, length.out = 1e4)
y<-func(x)
ggplot()+
geom_line(aes(x = x, y = y)) +
scale_y_continuous(limits = c(-100,100)) +
geom_hline(yintercept = 0)
ggplot()+
geom_line(aes(x = x, y = y)) +
scale_y_continuous(limits = c(-100,100),color = 'dodgerblue') +
geom_hline(yintercept = 0)
sample(c('H','T'),
size = 1)
ggplot()+
geom_line(aes(x = x, y = y)) +
scale_y_continuous(limits = c(-100,100),color = 'dodgerblue') +
geom_hline(yintercept = 0)
ggplot()+
geom_line(aes(x = x, y = y),color = 'dodgerblue') +
scale_y_continuous(limits = c(-100,100),) +
geom_hline(yintercept = 0)
ggplot()+
geom_line(aes(x = x, y = y),color = 'dodgerblue') +
scale_y_continuous(limits = c(0,50),) +
geom_hline(yintercept = 0)
sample(c('H','T'),
size = 1)
sample(c('H','T'),
size = 1)
#As we increase the number of flips, we generally get a proportion Heads close to 0.5
sample(c(1,0),
size = 100,
replace = T) %>%
mean()
#As we increase the number of flips, we generally get a proportion Heads close to 0.5
sample(c(1,0),
size = 100,
replace = T) %>%
mean()
#As we increase the number of flips, we generally get a proportion Heads close to 0.5
sample(c(1,0),
size = 100,
replace = T) %>%
mean()
#As we increase the number of flips, we generally get a proportion Heads close to 0.5
sample(c(1,0),
size = 100,
replace = T) %>%
mean()
#Graphically:
n<-100
#sample coinflips
coinflip<-data.frame(heads = sample(c(1,0),
size = n,
replace = T),
x = 1:n)%>%
#cumulative heads
mutate(prop_heads = cumsum(heads) / seq_along(heads))
View(coinflip)
ggplot(data = coinflip,
aes(x = x,
y = prop_heads))+
geom_line()+
labs(title = 'Coinflips: {timeframe}',
x = 'sample average',
y = 'probability')+
transition_time(x)+
ease_aes('linear')
library(gganimate)
ggplot(data = coinflip,
aes(x = x,
y = prop_heads))+
geom_line()+
labs(title = 'Coinflips: {timeframe}',
x = 'sample average',
y = 'probability')+
transition_time(x)+
ease_aes('linear')
p <- ggplot(data = coinflip, aes(x = x, y = prop_heads)) +
#Proportion heads
geom_line(color = "red", size = 1.2) +
# True probability line
geom_hline(yintercept = 0.5, color = "blue", linetype = "dashed", size = 1) +
# Current point
geom_point(color = "red", size = 2) +
# Set axis limits
ylim(0, 1) +
xlim(1, n) +
# Labels and title
labs(
title = 'Coin Flips Converging to True Probability',
x = 'Number of Flips',
y = 'Proportion of Heads',
caption = 'Red line: Sample proportion | Blue dashed: True probability (0.5)'
) +
# Styling
theme_minimal() +
theme(
plot.title = element_text(size = 16, hjust = 0.5, face = "bold"),
plot.subtitle = element_text(size = 12, hjust = 0.5),
plot.caption = element_text(size = 10),
axis.text = element_text(size = 10),
axis.title = element_text(size = 12)
) +
# Animation settings
transition_reveal(x) +
ease_aes('cubic-out')
anim <- animate(p,
width = 800,
height = 600,
fps = 15,
duration = 10,
renderer = gifski_renderer("coinflip_convergence.gif"))
anim <- animate(p,
width = 800,
height = 600,
fps = 15,
duration = 10,
renderer = gifski_renderer("../output/coinflip_convergence.gif"))
#flip 20 coins (weighted)
coins<-rbinom(n = 20, size = 1, prob = .3)%>%
as.data.frame()
#average heads
mean<-mean(coins$.)
#plot distribution
ggplot()+
geom_histogram(aes(x = coins$.))+
geom_vline(xintercept = mean)
#initialize
n<-10000
mean<-rep(NA,n)
#loop
for (i in 1:n) {
#flip 20 coins
coins<-rbinom(n = 20, size = 1, prob = .3)%>%
as.data.frame()
#store mean
mean[i]<-mean(coins$.)
}
means_df<-data.frame(mean = mean)
#the first 10...
ggplot()+
geom_histogram(aes(x = means_df[1:10,]))
#...100...
ggplot()+
geom_histogram(aes(x = means_df[1:100,]))
#...1000
ggplot()+
geom_histogram(aes(x = means_df[1:1000,]))
#...10000
ggplot()+
geom_histogram(aes(x = means_df[1:10000,]))
