Section # 01

Students must abide by UVic academic regulations and observe standards of scholarly integrity (i.e. no plagiarism or cheating). Therefore, this assignment must be taken individually and not with a friend, classmate, or group. You are also prohibited from sharing any information about the assignment with others. I affirm that I will not give or receive any aid on this assignment and that all work will be my own. W.Khunpluem

In environmental economics, perfectly competitive markets are our benchmark for efficiency: surplus is maximal under certain conditions:

The point of experiment 1 is to investigate how crucial are the assumptions of price taking and perfect knowledge. If the model of perfect competition does a good job of predicting behaviour even when these assumptions are not met, we can be more confident about using the model as our efficiency benchmark.

In experiment 1 the market-maker (in our case, the webserver) only makes use of the bids and asks when determining price, not the values and costs which are private information. Furthermore, your bid or ask may be the marginal bid or ask, determining either the price paid by demanders or the price received by suppliers. In a nutshell, we compare the predictions of perfect competition to the experimental outcomes to understand whether the assumptions of price taking and perfect information are substantive or merely for analytical convenience.

Recall that in experiment 1 buyers with bids weakly higher than the marginal bid purchase the good and suppliers with asks weakly lower than the marginal ask sell the good. We investigated two different market institutions regarding the prices paid and received. In one treatment the price paid by buyers was the marginal bid and the price received by sellers was the marginal ask: The difference between the marginal bid and marginal ask was the market maker’s gain per unit. In the other treatment the prices were reversed: the price paid by buyers was the marginal ask and the price received by sellers was the marginal bid. The difference between the marginal bid and marginal ask was the market maker’s loss per unit. The treatments were imbalanced in terms of the number of subjects: in rounds 1-10 most subjects were in the gain treatment, and then in rounds 11-20 most of the subjects were in the loss treatment. This allows us to look at the how the market institution (market-maker gain vs. loss) interacts with market depth.

1 (10 marks)

Suppose that you are a demander. If you are in the treatment where the market-maker makes a loss, could your bid ever determine the price buyers pay? If you are in the treatment where the market-maker makes a gain, could your bid ever determine the price buyers pay? How does market depth influence the probability your bid will determine the price buyers pay when the marketmaker gains?

If I am a demander and the market-maker makes a loss, my bid couldn’t determine the price buyers pay, but it would affect the probability of my trade. However, if the market-maker makes a gain, my bid could determine the price buyers pay because my bid price is more likely to become a marginal bid. Larger market depth leads to the less big trades affect the price. Therefore, more stable price would cause my bid to have lower chance to influence the price buyers pay and a shallow market bid has a higher chance to affect the price buyers pay.

2 (10 marks)

Create a function named salutations which takes as an argument person and prints a personalized greeting. The body of your function should be something like print(paste0("hello ",person,"!")). Hint: functions are defined like this function_name<-function(argument){body}. Functions are called like this: function_name("Rich")

salutations<-function(person){print(paste0("hello ",person,"!"))}
salutations("Lexi")
## [1] "hello Lexi!"

3 (0 marks)

Using dataframes supply_and_demand, bid_and_ask, and outcomes plot supply & demand, bids & asks and the marginal bid & ask for round_of_ten==10. Make the supply and demand prominent, and the bids and asks more subtle, and the marginal bid and ask as dots.

4 (10 marks)

Explain what each line of code does to create first_plot. Hint: help on what built-in functions do can by found by typing ?x in the console, where x is the name of the function. Suppose that you were interested in looking at all the rounds in round_of_ten, not just round 10. What would you modify in the code for first_plot for each different round? How many lines of code would you have if you cut/pasted/modified the code that created first_plot to create second_plot, third_plot… tenth_plot?

The “first_plot” has 6 lines of code. The first line “ggplot” describes the mapping. It variables map to the x and y axis and also controls the color. The second line “geom_step” adds the thick supply and demand curves. The third line “geom_step” adds the thin bids and asks curves. “lwd” is used to adjust the thickness of the curves. lwd=1.5 makes the supply and demand curves become thicker. Next, the Fourth line “geom_point” adds a layer of points to the plots which are the outcomes of the experiment. The fifth line “facet_grid” is separating the plot into a grid facet where the graph has the market makers making gain or loss and subplots are on the rounds. Finally, the sixth line “ggtitle” states the title for the plot, explaining what the curves and dots are. To modify the code for each different round, we need to change the ‘round_of_ten’ to “round_of_number”. We would have to create 3 more rounds of code per plot that is 27 lines of code to reach tenth_plot.

5 (10 marks)

Use the code that created first_plot to write a function plot_sd() that takes as an argument rnd_of_10. The body of plot_sd() will contain the code that created first_plot, except wherever there was a 10 you need to replace it with rnd_of_10. Hint: you can test the function by typing plot_sd(10) and it should look identical to first_plot. Use assert_that to make sure the input to your function is valid: i.e. rnd_of_10 %in% 1:10. How many lines of code would you have in total for the function, and 10 calls to that function?

plot_sd<-function(rnd_of_10){ggplot(mapping=aes(x=q, y=variable, colour=role))}+
    geom_step(data=filter(supply_and_demand, round_of_ten==rnd_of_10), direction="vh", alpha=.5, lwd=1.5)+
    geom_step(data=filter(bid_and_ask, round_of_ten==rnd_of_10), direction="vh", alpha=.5)+
    geom_point(data=filter(outcomes, round_of_ten==rnd_of_10), size=4, alpha=.4)+ 
    facet_grid(marketmaker~Rounds)+
ggtitle(paste0("Supply, Demand (thick), bids, asks (thin), Marginal bid, ask (dots) in round",rnd_of_10))

There are 7 lines of code in total. The first 6 lines are for the "plot_sd" function and the another 1 for testing the function.

6 (10 marks)

Use your function plot_sd() to plot the supply, demand bids and asks in round 10.

7 (10 marks)

Copy and paste the code that created first_plot, and rename it third_plot. Add the aesthetic frame=round_of_ten inside the aes(). i.e mapping=aes(q,variable, colour=role, frame=round_of_ten). Remove the filtering of the data used by both the geom_step() calls e.g instead of data=filter(supply_and_demand,round_of_ten==10) use data=supply_and_demand. Run the code to create third_plot by hitting control plus enter but do not look at at the plot. Instead, if third_plot ran without error, copy the following code into your assignment1.R file and hit control plus enter. How many lines of code were required to see the plots for all 10 rounds?

10 lines of code were required to see the plots for all 10 rounds.

(third_plot <- ggplotly(third_plot)%>%
  animation_opts(1000,transition = 100)%>%
  hide_legend()%>% 
  config(displayModeBar = F))

Insert your animation (third_plot) below:

8 (10 marks)

Describe the dataframe mydf. You can view the dataframe by clicking on it in the top right panel of Rstudio, and it will open in the top left panel. What are the columns, and what are the rows? The columns show the student’s data that are grouped as categories in rows. The rows include oneid, choice, profit, and etc. “Oneid” is the last 5 digits of students’s v number that are participating. “Choice” is choice of students to bid. “Profit” is how much they can get. “Variable” is cost of seller and value of buyer. “Round” is the round number. “Market maker” is the result that market makers make (gain or loss) “Role” is what the students are (seller or buyer). “Numtrans” is the number of transaction that occurs “Marginal bid” is a marginal bid. “Marginal ask” is a marginal ask. “Time” is the time they choose to start. “Rounds of ten” is a period of round 1-10. “Rounds” is telling when it occurs before ten round or after ten round ( like round 1-10 and round 11-20).

9 (10 marks)

Explain what the following code snippet does. Hints: <- is the assignment function (give the result on the right the name on the left). %>% is the pipe function, read as THEN (take what is on the left THEN apply the function on the next line). For functions that have alphabetic names you can type ?name in the console to get help. e.g. ?group_by

value_and_cost <- mydf%>%
    group_by(marketmaker,round_of_ten, Rounds, role, variable)%>%
    count(variable)

#The assignment function use "mydf" data to create "value and cost". Then, the pipe function takes the dataframe "value_and_cost" and put the 'group_by' function which create a grouped table that contains the wanted categories. The categories are market maker,round_of_ten, Rounds, role, and variable. 

10 (10 marks)

Cut and paste the above code snippet into your assignment1.R file and save. Source the assignment1.R and click on value_and_cost in the top right panel. How can dataframe value_and_cost be used to check to make sure that function plot_sd is plotting the supply and demand correctly? In the table, it shows the highest price of the buyer at 29 with the number of people at 1 that is on the demand line’s first step. The second high price is 28 with the number of people at 2 and the third high price is 27 with the number of people is 1. Therefore, when we look at the graph, we will see the data matches the charts correctly, meaning the function “plot_sd” is plotting the supply and demand correctly too.

11 (0 marks)

For round_of_ten==10, plot the relationship between choice and variable for all combinations of marketmaker and role, using a different colour for Rounds

12 (10 marks)

Copy and paste the code that created fourth_plot, and rename it fifth_plot. Add the aesthetic frame=round_of_ten inside the aes(). i.e mapping=aes(variable,choice,frame=round_of_ten, colour=Rounds). Remove the filtering of the data e.g. instead of data=filter(mydf, round_of_ten==10) use data=mydf. Run the code to create fifth_plot by hitting control plus enter but do not look at at the plot. Instead, if fifth_plot ran without error, copy the following code into your assignment1.R file and hit control plus enter.

(fifth_plot <- ggplotly(fifth_plot)%>%
  animation_opts(1000,transition = 100)%>%
  hide_legend()%>% 
  config(displayModeBar = F))