Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update main.Rmd #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
328 changes: 221 additions & 107 deletions main.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@
title: "FamiliaR with Dynamics"
output:
flexdashboard::flex_dashboard:
orientation: columns
orientation: column
social: menu
source_code: embed
theme: spacelab
runtime: shiny
---

<style>

.form-control {
width: 90%;
}

</style>


```{r global, include=FALSE}
# load data in 'global' chunk so it can be shared by all users of the dashboard
library(datasets)
library(ggplot2)
library(bslib)
options(shiny.maxRequestSize = 30*1024^2)
data(faithful)
Solow1 <- function(s,k,a){
Expand Down Expand Up @@ -75,10 +85,9 @@ conditionalPanel("input.model == 'Solow'",
sliderInput("d", label = "Depreciation:", min = 0, max = 1, value =0.02, step = 0.01),
sliderInput("n", label = "Population growth rate:", min = 0, max = 1, value =0.5, step = 0.01),
sliderInput("t", label = "Time", min = 0, max = 100, value =20, step = 5),
numericInput('k0', 'Initial capital, k0:', 3, min=0.5, max=200),
actionButton("go","(Re)start Numeric Simulation")
numericInput('k0', 'Initial capital, k0:', 3, min=0.5, max=200)

)

conditionalPanel("input.model == 'Ramsey'",
sliderInput("t", label = "Time", min = 0, max = 200, value = 50, step = 5),
sliderInput("c", label = "Consumption, dc/dt",min = 0, max = 5, value = 3, step = 0.05),
Expand All @@ -89,97 +98,190 @@ conditionalPanel("input.model == 'Ramsey'",
sliderInput("alpha", label = "Elasticity parameter (measuring the responsiveness of the output production to changes in the input capital):", min = 0, max = 1, value = 0.3, step = 0.01),

)
```

Column
-----------------------------------------------------------------------


### Initial values {data-height=350}
```{r}

####################################################
#Conditional panel for initial values for Solow model#
####################################################
conditionalPanel("input.model == 'Solow'",
sliderInput("initial_values", label="initial values", min=1, max=5, value=1, step=1),
conditionalPanel("input.initial_values == 1",
numericInput('aa1', 'a', 1 )),
conditionalPanel("input.initial_values == 2",
fillRow(numericInput('aa2', 'a', 1),
numericInput('bb2', 'b', 1))),
conditionalPanel("input.initial_values == 3",
fillRow(numericInput('aa3', 'a', 1),
numericInput('bb3', 'b', 1),
numericInput('cc3', 'c', 1))),
conditionalPanel("input.initial_values == 4",
fillRow(
numericInput('aa4', 'a', 1),
numericInput('bb4', 'b', 1),
numericInput('cc4', 'c', 1),
numericInput('dd4', 'd', 1))
),

conditionalPanel("input.initial_values == 5",
fillRow(
numericInput('aa5', 'a', 1),
numericInput('bb5', 'b', 1),
numericInput('cc5', 'c', 1),
numericInput('dd5', 'd', 1),
numericInput('ee5', 'e', 1))
),
actionButton("go","(Re)start Numeric Simulation", style="background-color: #ec7063 ; border-radius: 10px !important; border: none; position:absolute; bottom: 5px; margin: auto;"
))

######################################################
#Conditional panel for initial values for Goodwin model#
#####################################################
#y0 <- matrix(c(0.2, 1, 0.6, 1, 1.1, 1), ncol = 2, nrow = 3, byrow = TRUE)
conditionalPanel("input.model == 'Goodwin'",
fluidPage(
fluidRow(
sliderInput("initial_values_g",
label="initial values",
min=1,
max=3,
value=1,
step=1)
),
fluidRow(
conditionalPanel("input.initial_values_g == 1",
numericInput('gaa1', 'a', 0.2 ),
numericInput('gbb1', 'b', 1)),
conditionalPanel("input.initial_values_g == 2",
column(6,
numericInput('gaa2', 'a1', 0.2),
numericInput('gbb2', 'b1', 1)),
column(6,
numericInput('gcc2', 'a2',0.6),
numericInput('gdd2', 'b2',1))),
conditionalPanel("input.initial_values_g == 3",
column(4,
numericInput('gaa3', 'a1', 0.2),
numericInput('gbb3', 'b1', 1)),
column(4,
numericInput('gcc3', 'a2', 0.6),
numericInput('gdd3', 'b2', 1)),
column(4,
numericInput('gee3', 'a3', 0.8),
numericInput('gff3', 'b3', 1)))),
fluidRow(
actionButton("go2",
"(Re)start Numeric Simulation",
style="background-color: #ec7063 ; border-radius: 10px !important; border: none; position:absolute; bottom: 5px; margin: auto;"))))

```

Column
-----------------------------------------------------------------------




### Numerical Simulations

```{r}
renderPlot({
if (input$model == "Solow") {
req(input$go)
phaseR::flowField(solow_de,
xlim = c(0.0001, input$t),
ylim = c(0.0001, input$k),
parameters = c(input$s,input$a,input$d),
points=10,
system = 'one.dim',
add = F,
xlab = 't',
ylab = 'dk/dt')

phaseR::nullclines(solow_de,
xlim = c(0.0001, input$t),
ylim <- c(0.0001,input$k),
parameters = c(input$s,input$a,input$d),
system = "one.dim",
state.names = 'k')

phaseR::trajectory(solow_de,
y0 = input$k0,
tlim = c(0,500),
parameters = c(input$s,input$a,input$d),
system = "one.dim",
colour = rep("black"),
add = T)
}
else if (input$model == "Goodwin") {
parameters <- c(a = 1.5,
b = 1.8,
c = 0.8,
d = 0.8,
sigma=0.8)
state <- c(V = 0.5,
U = 1,
Q = 1)
times <- seq(0, 20, by = 0.01)
out <- deSolve::ode(y = state, times = times, func = goodwin2, parms = parameters)
par(oma = c(0, 0, 3, 0))
plot(out, xlab = "time", ylab = "-")
}

else if (input$model == "Ramsey"){
ramsey.flowField <- phaseR::flowField(
ramsey,
xlim = c(0.01,input$k),
ylim = c(0.01,input$c),
parameters = c(0.2,0.03,0.1,0.3),
points = 10,
add=F
)
grid()

ramsey.nullclines <- phaseR::nullclines(
ramsey,
xlim = c(0,input$k),
ylim = c(0.01,input$c),
state.names = c('k','c'),
parameters = c(0.2,0.03,0.1,0.3),
points = 500,
col = c("blue","red"),
add.legend=T
)

}

})
```
initial_val <- eventReactive(input$go, {
print(typeof(input$initial_values))
nval <- input$initial_values
print(paste("nval=",nval))
if(nval==1){return(c(input$aa1))}
else if (nval==2){return(c(input$aa2,input$bb2))}
else if(nval==3){return(c(input$aa3,input$bb3,input$cc3))}
else if(nval==4){return(c(input$aa4,input$bb4,input$cc4,input$dd4))}
else {return(c(input$aa5,input$bb5,input$cc5,input$dd5,input$ee5))}

# ifelse(nval==1, c(a),
# ifelse(nval==2, c(a,b),
# ifelse(nval==3, c(a,b,c),
# ifelse(nval==4, c(a,b,c,d),
# c(a,b,c,d,e)))))
})
initial_val_g <- eventReactive(input$go2, {
return(c(input$gaa2,input$gbb2))})

conditionalPanel("input.model == 'Solow'",
renderPlot({
ini <- initial_val()
phaseR::flowField(solow_de,
xlim = c(0.0001, input$t),
ylim = c(0.0001, input$k),
parameters = c(input$s,input$a,input$d),
points=10,
system = 'one.dim',
add = F,
xlab = 't',
ylab = 'dk/dt')
phaseR::nullclines(solow_de,
xlim = c(0.0001, input$t),
ylim <- c(0.0001,input$k),
parameters = c(input$s,input$a,input$d),
system = "one.dim",
state.names = 'k')
phaseR::trajectory(solow_de,
y0 = ini,
tlim = c(0, input$t),
parameters = c(input$s,input$a,input$d),
system = "one.dim",
add = T)}),
style="position: absolute; aspect-ratio: auto")

conditionalPanel("input.model == 'Goodwin'",
renderPlot({
ini_g <- initial_val_g()
parameters <- c(a = 1.5,
b = 1.8,
c = 0.8,
d = 0.8,
sigma=0.8)
state <- c(V = ini_g[1], #0.5
U = ini_g[2], #1
Q = 1)
times <- seq(0, 20, by = 0.01)
out <- deSolve::ode(y = state, times = times, func = goodwin2, parms = parameters)
par(oma = c(0, 0, 3, 0))
plot(out, xlab = "time", ylab = "-")}),
style="position: absolute; aspect-ratio: auto")

conditionalPanel("input.model == 'Ramsey'",
renderPlot({
phaseR::flowField(
ramsey,
xlim = c(0.01,input$k),
ylim = c(0.01,input$c),
parameters = c(0.2,0.03,0.1,0.3),
points = 10,
add=F)
#grid()
phaseR::nullclines(
ramsey,
xlim = c(0,input$k),
ylim = c(0.01,input$c),
state.names = c('k','c'),
parameters = c(0.2,0.03,0.1,0.3),
points = 500,
col = c("blue","red"),
add.legend=T)}),
style="position: absolute; aspect-ratio: auto")
```

Column {.tabset}
-----------------------------------------------------------------------
--------------------------------------------------------------------

### Dynamics

```{r}
#renderPrint(input$model)
#uiOutput("dynamic")
conditionalPanel("input.model == 'Solow'",
renderPlot({
k <- 1:input$k
if (input$model == "Solow") {
Expand All @@ -190,42 +292,54 @@ renderPlot({
#plot(k,Solow(input$s,k, input$a), type="l")
#lines(k,Solow(input$s,k, -input$a), type="l")
}
})
}),style="position: absolute; aspect-ratio: auto")
```

### Phase Portrait/Phase Diagram

```{r}
renderPlot({
if (input$model == "Solow"){
phaseR::phasePortrait(solow_de,
ylim <- c(0.5,input$k),
parameters = c(input$s,input$a,input$d),
col = 'black',
add.grid = T,
xlab='k',
ylab='dk/dt')
}
if (input$model == "Goodwin"){
phaseR::flowField(goodwin,
xlim = c(0,1.5),
ylim = c(0, 1.5),
parameters = c(0.25, 0.6, 0.2, 0.3, 0.4),
points = 19,
add = FALSE)
phaseR::nullclines(goodwin,
xlim = c(0,1.5),
ylim = c(0, 1.5),
parameters = c(0.25, 0.6, 0.2, 0.3, 0.4),
points = 500)
y0 <- matrix(c(0.2, 1, 0.6, 1, 1.1, 1), ncol = 2, nrow = 3, byrow = TRUE)
phaseR::trajectory(goodwin,
y0 = y0,
tlim = c(0,10),
parameters = c(0.25, 0.6, 0.2, 0.3, 0.4),
colour = rep("black", 3))
}
})
y_in <- eventReactive(input$go2, {
print(input$initial_values_g)
print(c(input$aa3, input$bb3, input$cc3, input$dd3, input$ee3, input$ff3))
if(input$initial_values_g==1){
return(matrix(c(input$gaa1, input$gbb1), ncol = 1, nrow = 2, byrow = TRUE))}
else if(input$initial_values_g==2){
return(matrix(c(input$gaa2, input$gbb2, input$gcc2, input$gdd2), ncol = 2, nrow = 2, byrow = TRUE))}
else{return(matrix(c(input$gaa3, input$gbb3, input$gcc3, input$gdd3, input$gee3, input$gff3), ncol = 3, nrow = 2, byrow = TRUE))}})

conditionalPanel("input.model == 'Solow'",
renderPlot({
phaseR::phasePortrait(solow_de,
ylim <- c(0.5,input$k),
parameters = c(input$s,input$a,input$d),
col = 'black',
add.grid = T,
xlab='k',
ylab='dk/dt')}),
style="position: absolute; aspect-ratio: auto")

conditionalPanel("input.model == 'Goodwin'",
renderPlot({
y0 <- y_in()
phaseR::flowField(goodwin,
xlim = c(0,1.5),
ylim = c(0, 1.5),
parameters = c(0.25, 0.6, 0.2, 0.3, 0.4),
points = 19,
add = FALSE,
xlab = "a",
ylab = "b")
phaseR::nullclines(goodwin,
xlim = c(0,1.5),
ylim = c(0, 1.5),
parameters = c(0.25, 0.6, 0.2, 0.3, 0.4),
points = 500)
phaseR::trajectory(goodwin,
y0 = y0,
tlim = c(0,10),
parameters = c(0.25, 0.6, 0.2, 0.3, 0.4),
colour = rep("black", 3))}),
style="position: absolute; aspect-ratio: auto")
```


Expand Down