```{library(ggplot2)}

update_frequency <- function(theta, N, s, mu, m, p_migrant) { #migrant allele frequency theta_migrant <- acos(sqrt(p_migrant))

#migration theta_mig <- (1 - m) * theta + m * theta_migrant

# Assuming direct selection on theta for simplification for demonstration #simulate the selection effect on theta, then convert to p for simplicity in handling selection p_sel <- cos(theta_mig)^2 + s * (cos(theta_mig)^2 * (1 - cos(theta_mig)^2))

# Ensure p_selection is within bounds [0, 1] p_sel <- max(min(p_sel, 1), 0)

# Genetic drift (binomial sampling) p_next_drift <- rbinom(1, size = 2N, prob = p_sel) / (2N)

# Convert p_next_drift back to theta for next generation theta_next <- acos(sqrt(p_next_drift))

return(theta_next) } N <- 1000 # assumed Population size p_initial <- 0.5 # Initial frequency of allele A theta_initial <- acos(sqrt(p_initial)) # Convert initial p to theta s <- 0.03 # Selection coefficient showing a simplified effect on p. mu <- 0.001 # Mutation rate (not used in trigonometric transformation) m <- 0.01 # simplified Migration rate p_migrant <- 0.01 # Frequency of A in migrants generations <- 1000 # Number of generations to simulate # Data frame to store simulation results in terms of theta results_trig <- data.frame(Generation = integer(), Theta = numeric(), p = numeric())

Running simulation using new theta

theta <- theta_initial for (gen in 1:generations) { theta <- update_frequency(theta, N, s, mu, m, p_migrant) p <- cos(theta)^2 # Convert theta back to p for recording results_trig <- rbind(results_trig, data.frame(Generation = gen, Theta = theta, p = p)) } # Plotting ggplot(results_trig, aes(x = Generation, y = p)) + geom_line() + labs(title = “Allele Frequency Dynamics with evolutionary forces.”, x = “Generation(s)”, y = “Frequency of Allele A (P)”) + theme_minimal()

#Now for the next step I will create a simple scenario simulating climate change representing #the integration of various evolutionary affects on theta.

Climate change effect

climate_change_effect <- function(generation) { if (generation >= 50 && generation <= 100) { # Example period of adverse climate change return(-0.005) # Negative impact on growth rate } else if (generation > 100 && generation <= 150) { # Period of favorable climate return(0.005) # Positive impact on growth rate } return(0) # No effect outside these periods }

Predation effect for simplicity, assuming a constant negative

predation_effect <- -0.002

Available food effect, for simplicity, assume it varies in known cycles

available_food_effect <- function(generation) { if (generation %% 30 < 15) { # Simulate periodic fluctuations in food availability return(0.002) # Positive impact on growth rate when food is abundant } else { return(-0.002) # Negative impact when food is scarce } }

Initialize parameters and results dataframe

growth_rate <- 0.01 # simple population growth rate per generation N <- 500 # Initial population size generations <- 200 # Total number of generations to simulate results_trig <- data.frame(Generation = integer(), Theta = numeric(), p = numeric(), PopulationSize = integer(), AdjustedGrowthRate = numeric())

theta <- acos(sqrt(p_initial)) # Assuming p initial is defined earlier as the initial allele frequency N_current <- N # Starting with initial population size

for (gen in 1:generations) { # Calculating the total adjusted growth rate for the current generation adjusted_growth_rate <- growth_rate + climate_change_effect(gen) + predation_effect + available_food_effect(gen)

# Updating population size for its current generation N_current <- N_current * (1 + adjusted_growth_rate)

# Maintaining population size as an integer by rounding N_current <- round(N_current)

# Update allele frequencies using the trigonometric HW M. theta <- update_frequency(theta, N_current, s, mu, m, p_migrant) # Using the updated N p <- cos(theta)^2 # Convert theta back to p

# Storing the results results_trig <- rbind(results_trig, data.frame(Generation = gen, Theta = theta, p = p, PopulationSize = N_current, AdjustedGrowthRate = adjusted_growth_rate)) } # Plotting allele frequency ggplot(results_trig, aes(x = Generation, y = p)) + geom_line(color = “blue”) + labs(title = “Allele Frequency Over Time”, x = “Generation”, y = “Frequency of Allele A”) + theme_minimal() # Plotting population size ggplot(results_trig, aes(x = Generation, y = PopulationSize)) + geom_line(color = “red”) + labs(title = “Population Size Over Time”, x = “Generation”, y = “Population”) + theme_minimal() # Plotting the adjusted growth rate over generations ggplot(results_trig, aes(x = Generation, y = AdjustedGrowthRate)) + geom_line(color = “purple”) + labs(title = “Population Growth Rate Over Time”, x = “Generation”, y = “Growth Rate”) + theme_minimal()

Plotting Theta over Generations

ggplot(results_trig, aes(x = Generation, y = Theta)) + geom_line(color = “darkgreen”) + labs(title = “GPA (3B8) Over Generations”, x = “Generation”, y = expression(Theta ~ “(Radians)”)) + theme_minimal() + theme(plot.title = element_text(hjust = 0.5))


## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:


```r
summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

```{library(ggplot2)}

update_frequency <- function(theta, N, s, mu, m, p_migrant) { #migrant allele frequency theta_migrant <- acos(sqrt(p_migrant))

#migration theta_mig <- (1 - m) * theta + m * theta_migrant

# Assuming direct selection on theta for simplification for demonstration #simulate the selection effect on theta, then convert to p for simplicity in handling selection p_sel <- cos(theta_mig)^2 + s * (cos(theta_mig)^2 * (1 - cos(theta_mig)^2))

# Ensure p_selection is within bounds [0, 1] p_sel <- max(min(p_sel, 1), 0)

# Genetic drift (binomial sampling) p_next_drift <- rbinom(1, size = 2N, prob = p_sel) / (2N)

# Convert p_next_drift back to theta for next generation theta_next <- acos(sqrt(p_next_drift))

return(theta_next) } N <- 1000 # assumed Population size p_initial <- 0.5 # Initial frequency of allele A theta_initial <- acos(sqrt(p_initial)) # Convert initial p to theta s <- 0.03 # Selection coefficient showing a simplified effect on p. mu <- 0.001 # Mutation rate (not used in trigonometric transformation) m <- 0.01 # simplified Migration rate p_migrant <- 0.01 # Frequency of A in migrants generations <- 1000 # Number of generations to simulate # Data frame to store simulation results in terms of theta results_trig <- data.frame(Generation = integer(), Theta = numeric(), p = numeric())

Running simulation using new theta

theta <- theta_initial for (gen in 1:generations) { theta <- update_frequency(theta, N, s, mu, m, p_migrant) p <- cos(theta)^2 # Convert theta back to p for recording results_trig <- rbind(results_trig, data.frame(Generation = gen, Theta = theta, p = p)) } # Plotting ggplot(results_trig, aes(x = Generation, y = p)) + geom_line() + labs(title = “Allele Frequency Dynamics with evolutionary forces.”, x = “Generation(s)”, y = “Frequency of Allele A (P)”) + theme_minimal()

#Now for the next step I will create a simple scenario simulating climate change representing #the integration of various evolutionary affects on theta.

Climate change effect

climate_change_effect <- function(generation) { if (generation >= 50 && generation <= 100) { # Example period of adverse climate change return(-0.005) # Negative impact on growth rate } else if (generation > 100 && generation <= 150) { # Period of favorable climate return(0.005) # Positive impact on growth rate } return(0) # No effect outside these periods }

Predation effect for simplicity, assuming a constant negative

predation_effect <- -0.002

Available food effect, for simplicity, assume it varies in known cycles

available_food_effect <- function(generation) { if (generation %% 30 < 15) { # Simulate periodic fluctuations in food availability return(0.002) # Positive impact on growth rate when food is abundant } else { return(-0.002) # Negative impact when food is scarce } }

Initialize parameters and results dataframe

growth_rate <- 0.01 # simple population growth rate per generation N <- 500 # Initial population size generations <- 200 # Total number of generations to simulate results_trig <- data.frame(Generation = integer(), Theta = numeric(), p = numeric(), PopulationSize = integer(), AdjustedGrowthRate = numeric())

theta <- acos(sqrt(p_initial)) # Assuming p initial is defined earlier as the initial allele frequency N_current <- N # Starting with initial population size

for (gen in 1:generations) { # Calculating the total adjusted growth rate for the current generation adjusted_growth_rate <- growth_rate + climate_change_effect(gen) + predation_effect + available_food_effect(gen)

# Updating population size for its current generation N_current <- N_current * (1 + adjusted_growth_rate)

# Maintaining population size as an integer by rounding N_current <- round(N_current)

# Update allele frequencies using the trigonometric HW M. theta <- update_frequency(theta, N_current, s, mu, m, p_migrant) # Using the updated N p <- cos(theta)^2 # Convert theta back to p

# Storing the results results_trig <- rbind(results_trig, data.frame(Generation = gen, Theta = theta, p = p, PopulationSize = N_current, AdjustedGrowthRate = adjusted_growth_rate)) } # Plotting allele frequency ggplot(results_trig, aes(x = Generation, y = p)) + geom_line(color = “blue”) + labs(title = “Allele Frequency Over Time”, x = “Generation”, y = “Frequency of Allele A”) + theme_minimal() # Plotting population size ggplot(results_trig, aes(x = Generation, y = PopulationSize)) + geom_line(color = “red”) + labs(title = “Population Size Over Time”, x = “Generation”, y = “Population”) + theme_minimal() # Plotting the adjusted growth rate over generations ggplot(results_trig, aes(x = Generation, y = AdjustedGrowthRate)) + geom_line(color = “purple”) + labs(title = “Population Growth Rate Over Time”, x = “Generation”, y = “Growth Rate”) + theme_minimal()

Plotting Theta over Generations

ggplot(results_trig, aes(x = Generation, y = Theta)) + geom_line(color = “darkgreen”) + labs(title = “GPA (3B8) Over Generations”, x = “Generation”, y = expression(Theta ~ “(Radians)”)) + theme_minimal() + theme(plot.title = element_text(hjust = 0.5))

```

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.