Docsity

Assignment 1 applied econometrics, Schemi e mappe concettuali di Econometria

Toggle sidebar
bg1
Exercise class 1
Introduction to RStudio
Legend:
Code
Output
Comments
1) Uploading datasets
You may use the function read.csv to upload a csv dataset saved in your computer. Use
arguments to specify specific features of the dataset you are uploading (see below).
Dataset <- read.csv(file.choose(), header = TRUE, sep=";", dec=",")
Name you are assigning to your dataset
It allows to choose a file interactively
Input file's first line as header
that specifies table's column names
“;” as data separator
“,” as decimals separator
The function View is a data viewer that allows you to look inside data frames. It invokes
a spreadsheet-style data viewer opened in a separated window.
Tip: by looking at your data you can check whether the dataset has been uploaded
correctly, i.e., whether it is showing the expected number of columns, if values are
properly separated and if decimals are divided by “.”.
View(Dataset)
Insert as argument the name of the object you want to view
pf3
pf4
pf5
pf8
pf9

Anteprima parziale del testo

Scarica Assignment 1 applied econometrics e più Schemi e mappe concettuali in PDF di Econometria solo su Docsity!

Exercise class 1 Introduction to RStudio Legend: Code Output Comments

  1. Uploading datasets You may use the function read.csv to upload a csv dataset saved in your computer. Use arguments to specify specific features of the dataset you are uploading (see below). Dataset <- read.csv(file.choose(), header = TRUE, sep=";", dec=",") Name you are assigning to your dataset It allows to choose a file interactively Input file's first line as header that specifies table's column names “;” as data separator “,” as decimals separator The function View is a data viewer that allows you to look inside data frames. It invokes a spreadsheet-style data viewer opened in a separated window. Tip: by looking at your data you can check whether the dataset has been uploaded correctly, i.e., whether it is showing the expected number of columns, if values are properly separated and if decimals are divided by “.”. View(Dataset) Insert as argument the name of the object you want to view
  1. Manipulating datasets Use the function is.na to verify the presence of null values in your dataset. If the output returns “FALSE” there is no null value, “TRUE” if otherwise. is.na(Dataset) X PSV...MWh PUN...MWh [1,] FALSE FALSE FALSE [2,] FALSE FALSE FALSE [3,] FALSE FALSE FALSE [4,] FALSE FALSE FALSE [5,] FALSE FALSE FALSE …… [ 69 ,] FALSE TRUE TRUE …… [92,] FALSE TRUE TRUE [93,] FALSE TRUE TRUE Null values in column 2 and 3 starting from row 69 [94,] FALSE TRUE TRUE [95,] FALSE TRUE TRUE [96,] FALSE TRUE TRUE The function tail is used to get the last parts of a vector, matrix, table or data frame. Specify the name of the object you want to get and the number of rows. tail(Dataset,3) X PSV...MWh PUN...MWh 94 NA NA 95 NA NA You get the last 3 rows of the object “Dataset” 96 NA NA As you can see from the output above, the last 3 rows contain null values (same as shown with function is.na ). You can remove them by overwriting your object “Dataset” with a new table that contains just the first 68 rows of your previous dataset.

If you write the name of any object (e.g. the variable just created “t”) and run the code, it returns values contained in the object in your console. t [1] "2017- 01 - 01" "2017- 02 - 01" "2017- 03 - 01" "2017- 04 - 01" "2017- 05 - 01" "2017- 06 - 01" "2017- 07 - 01" [8] "2017- 08 - 01" "2017- 09 - 01" "2017- 10 - 01" "2017- 11 - 01" "2017- 12 - 01" "2018- 01 - 01" "2018- 02 - 01" [50] "2021- 02 - 01" "2021- 03 - 01" "2021- 04 - 01" "2021- 05 - 01" "2021- 06 - 01" "2021- 07 - 01" "2021- 08 - 01" [57] "2021- 09 - 01" "2021- 10 - 01" "2021- 11 - 01" "2021- 12 - 01" "2022- 01 - 01" "2022- 02 - 01" "2022- 03 - 01" [64] "2022- 04 - 01" "2022- 05 - 01" "2022- 06 - 01" "2022- 07 - 01" "2022- 08 - 01" class(t) [1]"Date" Before plotting and computing any calculation, be sure the class of the variables you are using is “numeric”. class(Dataset$Electricity price) [1] "numeric"

  1. Drawing graphs You may create new variables, called “PSV” and “PUN”, assigning values of second and third column of your dataset (called “Dataset”). PSV <- Dataset[,2] You can use square brackets to refer to a specific point in a table. Coordinates are given by [row, column]. If you do not indicate any row, variable “PSV” will contain values of all 68 rows and second column of Dataset. PUN <- Dataset[,3] PUN [1] 72.2 55.5 44.5 42.9 43.1 48.9 50.3 55.8 48.6 54.7 65.8 65.1 49.0 57.0 56. 9 49.4 53. [18] 57.3 62.7 67.7 76.3 73.9 66.6 65.2 67.7 57.7 52.9 53.4 50.7 48.6 52.3 49. 5 51.2 52. [35] 48.2 43.3 47.5 39.3 32.0 24.8 21.8 28.0 38.0 40.3 48.8 43.6 48.7 54.0 60. 7 56.6 60. [52] 69.0 69.9 84.8 102.7 112.4 158.6 217.6 226.0 281.2 224.5 211.7 308.1 246. 230.1 271.3 441.6 543. Use the function plot to draw a time series graph. Use arguments to add elements to your graph. plot ( t, PUN, type="l", col="red", ylab = "PUN, PSV fwd price", ylim = c(0,600) ) x,y variables type of line (“l”=continuous line) line color y-axis label y-axis min and max limits

You may compute returns by using the log-difference proxy 𝑟𝑡 =

≅ ln

ReturnsPUN <- diff(log(Dataset[,3])) ReturnsPSV <- diff(log(Dataset[,2])) Drawing a scatter plot of Gas and Electricity price returns allows you to infer the relationship between the two variables (in this case positive and linear). plot(ReturnsPSV, ReturnsPUN, col="blue", xlab="ReturnsPSV (%)", ylab="ReturnsPUN (%)") title("Scatter plot of PUN and PSV returns 2017-2022")

  1. Computing correlation and variance How do you interpret this result? cor(ReturnsPUN, ReturnsPSV) [1] 0. cor(diff(log(PUN)),diff(log(PSV))) [1] 0. sd(y)*(12^0.5) [1] 0.
  2. Repeating the analysis selecting a different time period Given the variables under analysis (gas and electricity price) it may be interesting to compare January 20 17 - September 2022 results with those limited to February 2022- September 2022. What do you notice? What is the possible explanation? subDataset <- tail(Dataset,7) subDataset[1,1] [1] "01/02/2022" subDataset Data Gas price Electricity price 62 01/02/2022 81.5 211. 63 01/03/2022 126.6 308. 64 01/04/2022 100.2 246. 65 01/05/2022 89.8 230. 66 01/06/2022 102.7 271. 67 01/07/2022 172. 9 441. 68 01/08/2022 233.5 543.