Last active
January 4, 2016 19:59
-
-
Save gadenbuie/8670772 to your computer and use it in GitHub Desktop.
Simple Linear First-Order Difference Equations for Cobweb Model (Shiny App)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
# s(k) = s(0) + b*p(k-1) | |
# d(k) = d(0) - a*p(k) | |
# p(k) = -b/a*p(k-1) + (d0-s0)/a | |
cobweb = function(params){ | |
# Create data frame of prices, and supply and demand stocks | |
s0 = params[1] | |
d0 = params[2] | |
a = params[3] | |
b = params[4] | |
p0 = params[5] | |
k = params[6] | |
# Initialize data frame as model | |
model = data.frame(p = p0, s = s0, d = d0) | |
# Iteratively calculate price and supply and demand for k iterations | |
for(i in 1:k){ | |
# Calculate price at p(i+1) | |
p_next = -b/a*model[i,'p'] + (d0 - s0)/a | |
# Calculate demand at time i+1 from price at p(i+1) | |
d_next = d0 - a * p_next | |
# Calculate supply at time i+1 from price at p(i) | |
s_next = s0 + b * model[i, 'p'] | |
# Add new values to our model data | |
model = rbind(model, c(p_next, d_next, s_next)) | |
} | |
return(model) | |
} | |
plot_cobweb = function(params){ | |
s0 = params[1] | |
d0 = params[2] | |
a = params[3] | |
b = params[4] | |
p0 = params[5] | |
k = params[6] | |
# Create the model data up to k iterations | |
model = cobweb(params) | |
# Initialize plot | |
plot_max = round((max(model)+5)/5)*5 | |
plot(c(0, plot_max), c(0, plot_max), ann=F, bty='n', type='n') | |
title(xlab='Price', ylab='Units') | |
# Plot supply-price curve | |
abline(s0, b, col='blue', lwd=2) | |
text(0, s0, 'Supply', col='blue', pos=3) | |
# Plot demand-price curve | |
abline(d0, -a, col='orange', lwd=2) | |
text(0, d0, 'Demand', col='orange', pos=3) | |
# Add cobweb | |
for(i in 1:k){ | |
# Line segment from [p(i), s(i+1)] to [p(i+1), d(i+1)] | |
segments(x0 = model[i, 'p'], y0 = model[(i+1), 's'], | |
x1 = model[i+1, 'p'], y1 = model[(i+1), 'd'], | |
col=ifelse(i==k, 'red', 'black')) | |
# Line segment from [p(i+1), d(i+1)] to [p(i+1), s(i+2)] | |
segments(x0 = model[i+1, 'p'], y0 = model[i+1, 'd'], | |
x1 = model[i+1, 'p'], y1 = model[i+2, 's']) | |
} | |
} | |
# Shiny server functions that capture user input | |
# and call the functions above when user changes inputs | |
shinyServer(function(input,output) { | |
sliderValues <- reactive({ | |
c(input$s0, input$d0, input$a, input$b, input$p0, input$k) | |
}) | |
output$cobwebPlot <- renderPlot({ | |
plot_cobweb(sliderValues()) | |
}) | |
output$cobwebTable <- renderTable({ | |
cobweb(sliderValues()) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
# Define UI for dataset viewer application | |
shinyUI(pageWithSidebar( | |
# Application title | |
headerPanel("Cobweb Model"), | |
sidebarPanel( | |
sliderInput('s0', 'Initial Supply s(0):', | |
min = 0, max = 25, step = 1, value = 1), | |
sliderInput('d0', 'Initial Demand d(0):', | |
min = 0, max = 25, step = 1, value = 12), | |
sliderInput('a', 'Demand price sensitivity (a):', | |
min = 0, max = 2, step = 0.01, value = 1, ticks=F), | |
sliderInput('b', 'Supply price sensitivity (b):', | |
min = 0, max = 2, step = 0.01, value = 0.7, ticks=F), | |
sliderInput('p0', 'Initial price p(0):', | |
min = 0, max = 25, step = 0.25, value = 14), | |
sliderInput('k', 'Iterations', | |
min = 1, max = 15, step = 1, value = 1, | |
animate=animationOptions(interval = 500, loop=T)), | |
helpText('Based on the discrete-time cobweb model.', | |
'Seems to work best if you set iterations to ~10', | |
'and then explore parameters. Press the play triangle', | |
'under iterations to cycle through each step.') | |
), | |
mainPanel( | |
tabsetPanel( | |
tabPanel('Plot', plotOutput('cobwebPlot')), | |
tabPanel('Table', tableOutput('cobwebTable')) | |
) | |
) | |
)) |
If you happen to get a download has nonzero exit status
when you run the runGist
command, then run the following and try again.
options('download.file.extra' = '--no-check-certificate')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run this app, first make sure that you have R installed.
Then, at the command line (using Terminal.app on Mac or by running
cmd
on Windows), typeR
and hit enter to start R.Once in R, make sure you have Shiny installed by running
To start up a local web sever so that you can view the Cobweb Model shiny app, just run