-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogistic_US_Canada.R
More file actions
69 lines (55 loc) · 2.1 KB
/
Copy pathlogistic_US_Canada.R
File metadata and controls
69 lines (55 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#############################################
# Logistic Arms Race Model for Canada and U.S
##############################################
#time values
t_vals <- seq(0,350, by = 0.1)
#parameters e and f
e <- 0.733582888
f <- -0.59771515
#capacity limits for military spending
#Kp for Canada and Kg for U.S
Kp <- 40000
Kg <- 1041000
#initial conditions
#p is Canada and g is U.S
p0 <- 0
g0 <- 0
#create matrix to store model values
#columns are: model for Canada, model for US, time
arms <- matrix(NA, nrow = length(t_vals), ncol = 3)
arms[1,] <- c(p0, g0,1)
#use Euler's method to numerically simulate the model
arms_model <- function(t_vals, arms, a, b, c, d, e, f, Kp, Kg){
h <- t_vals[2] - t_vals[1]
for(t in 2:length(t_vals)){
p_prev <- arms[t-1, 1]
g_prev <- arms[t-1, 2]
t_prev<-arms[t-1,3]
#Logistic model equations
dp <- a*(1 - p_prev/Kp)*g_prev - c*p_prev + e
dg <- b*(1 - g_prev/Kg)*p_prev - d*g_prev + f
p_new <- p_prev + h*dp
g_new <- g_prev + h*dg
t_new<-t_prev+1
arms[t,] <- c(p_new, g_new,t_new)
}
return(arms)
}
#plot the model
model1 <- arms_model(t_vals, arms, a = 0.001025041, b = 3.06035844, c = 0.006444388, d = 0.01104621, e, f, Kp, Kg)
model1<-data.frame(model1)
colours <- c( "U.S" = "royalblue","Canada" = "red")
library(ggplot2)
ggplot(model1) +
geom_line(mapping = aes(x=X3, y = X1, colour = "Canada"), lwd = 1) +
geom_line(mapping = aes(x=X3, y = X2, colour = "U.S"), lwd = 1) +
labs(x = "Time", y = "Military Spending (Constant 2023 USD millions)", title = "Military Spending in Canada and U.S",
colour = "Legend") + scale_colour_manual(values = colours) +
theme(axis.title.x = element_text(family = "sans", size = 12, margin=margin(10,0,0,0)),
axis.title.y = element_text(family = "sans", size = 12, margin=margin(0,10,0,0)),
plot.title = element_text(family = "sans", size = 14, margin=margin(0,0,10,0)))
#to find the second numerical simulation for Canada and U.S
#we change the initial conditions and time values and run the code again
p0 <- 29065.9
g0 <-968381.6
t_vals <- seq(0,30, by = 0.1)