LaborCapitalAndTheOptimalGr.../CobbDouglas/plots/10000years/.Rhistory

513 lines
16 KiB
R
Executable File

k1_reverse_shooting = k1_forward_shooting
## Artfully guessed, such that s1 < 1
stepsize = 0.1
## stepsize = 0.1 => seconds (7s).
## stepsize = 0.01 => minutes (3 mins).
r_stepsize = ((1+r_1)^stepsize)-1
first = 0
last = 10000
times_forward_shooting = seq(from=first, to=last, by=stepsize)
times_reverse_shooting = seq(from=last, to=first, by=-stepsize)
### Initial conditions. Correspond to "year 0".
x1_init = 10^10 ## 1 billion? 15 billion? 100 billion? Too much?
x3_init = 10^5
# Forward shooting
options(digits=7)
## Evolution
x1_array_forward_shooting <- c()
x3_array_forward_shooting <- c()
a1_array_forward_shooting <- c()
a3_array_forward_shooting <- c()
s1_array_forward_shooting <- c()
s3_array_forward_shooting <- c()
x1_t = x1_init
x3_t = x3_init
#stepsize
comienzo = Sys.time()
max = max(times_forward_shooting)
for(t in times_forward_shooting){
if((100*t/max) %in% seq(from=0, to=100, by=1)){
cat(paste(floor(100*t/max), "%", sep=""))
cat("\n")
}
a1_t = a1(t)
a3_t = a3(t)
s1_t = s1(t)
s3_t = s3(t)
x1_t = x1_t*(1+r_stepsize) + dx1(t)*stepsize
x3_t = x3_t + dx3(t)*stepsize
a1_array_forward_shooting <- c(a1_array_forward_shooting, a1_t)
a3_array_forward_shooting <- c(a3_array_forward_shooting, a3_t)
s1_array_forward_shooting <- c(s1_array_forward_shooting, s1_t)
s3_array_forward_shooting <- c(s3_array_forward_shooting, s3_t)
x1_array_forward_shooting <- c(x1_array_forward_shooting, x1_t)
x3_array_forward_shooting <- c(x3_array_forward_shooting, x3_t)
}
fin = Sys.time()
fin-comienzo
## Checking condition
options(digits=22)
l = length(times_forward_shooting)
x1_array_forward_shooting[l]
x1_growth = (x1_array_forward_shooting[l]-x1_array_forward_shooting[l-1])/x1_array_forward_shooting[l-1]/(1*stepsize)
x1_growth
sum(x1_array_forward_shooting<0)
x3_array_forward_shooting[l]
x3_growth = (x3_array_forward_shooting[l]-x3_array_forward_shooting[l-1])/x3_array_forward_shooting[l-1]/(1*stepsize)
x3_growth
sum(x3_array_forward_shooting<0)
a1_growth = (a1_array_forward_shooting[l]-a1_array_forward_shooting[l-1])/a1_array_forward_shooting[l-1]/stepsize
a1_growth
a3_growth = (a3_array_forward_shooting[l]-a3_array_forward_shooting[l-1])/a3_array_forward_shooting[l-1]/stepsize
a3_growth
a1_array_forward_shooting[l]/x1_array_forward_shooting[l]
plot((a1_array_forward_shooting/x1_array_forward_shooting)[(l-100)-l])
# Plotting
## install.packages("tidyverse") <- Not totally necessary.
## install.packages("ggplot2")
## install.packages("ggsci")
library(ggplot2)
library(ggsci)
## General variables
saveplots=TRUE
times = times_forward_shooting
## Also for reverse shooting plots; time in reverse shooting is inverted
shootingtype="reverse"#"forward"#
shootingtype="forward"#"reverse"#
# directory = "/home/nuno/Documents/core/SRF/BackShooting/RCode/plots/temp"
directory = paste("/home/nuno/Documents/core/SRF/BackShooting/RCode/plots/satisfactory3/", shootingtype, "shooting", "/", last, "years", sep="")
directory
setwd(directory)
if(shootingtype=="forward"){
x1_array_plotting <- x1_array_forward_shooting
x3_array_plotting <- x3_array_forward_shooting
a1_array_plotting <- a1_array_forward_shooting
a3_array_plotting <- a3_array_forward_shooting
s1_array_plotting <- s1_array_forward_shooting
s3_array_plotting <- s3_array_forward_shooting
} else if(shootingtype=="reverse"){
x1_array_plotting <- x1_array_reverse_shooting
x3_array_plotting <- x3_array_reverse_shooting
a1_array_plotting <- a1_array_reverse_shooting
a3_array_plotting <- a3_array_reverse_shooting
s1_array_plotting <- s1_array_reverse_shooting
s3_array_plotting <- s3_array_reverse_shooting
}
height = 5
width = floor(height*(1+sqrt(5))/2)
imagenumbercounter = 1
saveplot = function(imagename){
if(saveplots){
ggsave(paste(imagenumbercounter,"_", imagename, "_",shootingtype, "shooting",".png", sep =""), units="in", width=width, height=height)
imagenumbercounter <<- imagenumbercounter+1
## https-//stackoverflow.com/questions/1236620/global-variables-in-r
}
}
options(digits=1) ## Just for display
## x1 and x3
xs <- list()
xs$values <- c(x1_array_plotting, x3_array_plotting)
xs$var <- c(rep("x1", length(x1_array_plotting)), rep("x3", length(x3_array_plotting)))
xs$times = rep(times,2)
xs <- as.data.frame(xs)
title_text = "Evolution of state variables"
(ggplot(data = xs, aes(x = times, y= values, color = var)) +
geom_line(size = 0.5)
+ labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Capital and labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
+ scale_color_lancet(
breaks=c("x1", "x3"),
labels=c("Capital", "Labor")
)
+ scale_y_continuous(breaks = seq(min(x1_array_plotting), max(x1_array_plotting), length.out=5))
)
saveplot("StateVariablesX1X3")
## x1 and x3- log plot
title_text = "Evolution of state variables"
(ggplot(data = xs, aes(x = times, y= values, color = var)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
#subtitle="n =303",
x="Year since start",
y="Capital and labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
+ scale_color_lancet(
breaks=c("x1", "x3"),
labels=c("Capital", "Labor")
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("StateVariablesX1X3logplot")
## Only x3
equis3 <- list()
equis3$values <- c(x3_array_plotting)
equis3$times = times
equis3 <- as.data.frame(equis3)
title_text = "Evolution of movement size"
(ggplot(data = equis3, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
)
saveplot("StateVariableX3")
## s1, s3, s4
sigmas <- list()
s4_array_plotting=1-s1_array_plotting-s3_array_plotting
sigmas$values <- c(s1_array_plotting, s3_array_plotting,s4_array_plotting)
sigmas$fraction <- c(rep("s1", length(s1_array_plotting)), rep("s3", length(s3_array_plotting)),rep("s4", length(s3_array_plotting)))
sigmas$times = rep(times,3)
sigmas <- as.data.frame(sigmas)
title_text = "Evolution of labor fractions"
(ggplot(data = sigmas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Fraction of total labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1", "s3", "s4"),
labels=c("Direct work", "Movement building", "Money-making")
)
)
saveplot("MovementFractions")
## s1 and s3
sigmas <- list()
sigmas$values <- c(s1_array_plotting, s3_array_plotting)
sigmas$fraction <- c(rep("s1", length(s1_array_plotting)), rep("s3", length(s3_array_plotting)))
sigmas$times = rep(times,2)
sigmas <- as.data.frame(sigmas)
title_text = "Evolution of labor fractions"
subtitle_text = "(only direct work and movement building)"
(ggplot(data = sigmas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle=subtitle_text,
x="Year since start",
y="Fraction of total labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1", "s3"),
labels=c("Direct work", "Movement building")
)
)
saveplot("MovementFractionsS1S3")
## Just direct work.
sigma1 <- list()
sigma1$values <- c(s1_array_plotting)
sigma1$times = times
sigma1 <- as.data.frame(sigma1)
title_text = "Evolution of direct work \nas a fraction of total labor"
(ggplot(data = sigma1, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Fraction of total labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
)
saveplot("MovementFractionsS1")
## Just movement building
sigma3 <- list()
sigma3$values <- c(s3_array_plotting)
sigma3$times = times
sigma3 <- as.data.frame(sigma3)
title_text = "Evolution of movement building\nas a fraction of total labor"
(ggplot(data = sigma3, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Fraction of labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
)
saveplot("MovementFractionsS3")
## Just direct work - logplot
sigma1 <- list()
sigma1$values <- c(s1_array_plotting)
sigma1$times = times
sigma1 <- as.data.frame(sigma1)
title_text = "Evolution of direct work \nas a fraction of labor"
(ggplot(data = sigma1, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
x="Year since start",
y="Fraction of labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
+ scale_y_continuous(trans = 'log2')
)
saveplot("MovementFractionsS1logplot")
## Just movement building
sigma3 <- list()
sigma3$values <- c(s3_array_plotting)
sigma3$times = times
sigma3 <- as.data.frame(sigma3)
title_text = "Evolution of movement building\nas a fraction of labor"
(ggplot(data = sigma3, aes(x = times, y= values))
+geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
x="Year since start",
y="Fraction of total labor"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.box="vertical"
)
##+ scale_color_lancet()
+ scale_y_continuous(trans = 'log2')
)
saveplot("MovementFractionsS3logplot")
## sigma_1*x3 and sigma3*x3, sigma_4*x3,
sigmaxs <- list()
sigmaxs$values <- c(s1_array_plotting*x3_array_plotting, s3_array_plotting*x3_array_plotting, s4_array_plotting*x3_array_plotting)
sigmaxs$fraction <- c(rep("s1x3", length(s1_array_plotting)), rep("s3x3", length(s3_array_plotting)), rep("s4x3", length(s3_array_plotting)))
sigmaxs$times = rep(times,3)
sigmaxs <- as.data.frame(sigmaxs)
title_text = "Evolution of labor\n in absolute terms"
(ggplot(data = sigmaxs, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Absolute number\nof movement participants"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1x3", "s3x3", "s4x3"),
labels=c("Direct work", "Movement building", "Money-making")
)
)
saveplot("MovementParticipantNumbersS1S3S4")
## sigma_1*x3 and sigma3*x3
sigmaxs <- list()
sigmaxs$values <- c(s1_array_plotting*x3_array_plotting, s3_array_plotting*x3_array_plotting)
sigmaxs$fraction <- c(rep("s1x3", length(s1_array_plotting)), rep("s3x3", length(s3_array_plotting)))
sigmaxs$times = rep(times,2)
sigmaxs <- as.data.frame(sigmaxs)
title_text = "Evolution of movement participants\nin absolute terms"
(ggplot(data = sigmaxs, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(only direct workers and movement builders)",
x="Year since start",
y="Absolute number\nof movement participants"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1x3", "s3x3"),
labels=c("Direct work", "Movement building")
)
)
saveplot("MovementParticipantNumbersS1S3")
## sigma_1*x3 and sigma3*x3 logplot
sigmaxs <- list()
sigmaxs$values <- c(s1_array_plotting*x3_array_plotting, s3_array_plotting*x3_array_plotting)
sigmaxs$fraction <- c(rep("s1x3", length(s1_array_plotting)), rep("s3x3", length(s3_array_plotting)))
sigmaxs$times = rep(times,2)
sigmaxs <- as.data.frame(sigmaxs)
title_text = "Evolution of movement participants\nin absolute terms \n(logarithmic scale)"
(ggplot(data = sigmaxs, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(only direct workers and movement builders)",
x="Year since start",
y="Absolute number\nof movement participants"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("s1x3", "s3x3"),
labels=c("Direct work", "Movement building")
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("MovementParticipantNumbersS1S3logplot")
## alpha1 and alpha3
alphas <- list()
alphas$values <- c(a1_array_plotting, a3_array_plotting)
alphas$fraction <- c(rep("a1", length(s1_array_plotting)), rep("a2", length(s3_array_plotting)))
alphas$times = rep(times,2)
alphas <- as.data.frame(alphas)
title_text = "Evolution of spending"
(ggplot(data = alphas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
#subtitle="n =303",
x="Year since start",
y="Spending"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("a1", "a2"),
labels=c("Direct spending","Spending on movement building")
)
)
saveplot("SpendingA1A3")
## alpha1 and alpha3- log plot
alphas <- list()
alphas$values <- c(a1_array_plotting, a3_array_plotting)
alphas$fraction <- c(rep("a1", length(s1_array_plotting)), rep("a2", length(s3_array_plotting)))
alphas$times = rep(times,2)
alphas <- as.data.frame(alphas)
title_text = "Evolution of spending"
(ggplot(data = alphas, aes(x = times, y= values, color = fraction)) +
geom_line(size = 0.5)
+labs(
title=title_text,
subtitle="(logarithmic scale)",
x="Year since start",
y="Spending"
#caption="@EA Mental Health Survey"
)
+theme(
legend.title = element_blank(),
#plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
#axis.text.y = element_blank(),
legend.position="bottom",
legend.direction="vertical"
)
+ scale_color_lancet(
breaks=c("a1", "a2"),
labels=c("Direct spending","Spending on movement building")
)
+ scale_y_continuous(trans = 'log2')
)
saveplot("SpendingA1A3logplot")