Firstly we are importing required libraries and loading data from local file. We use em_price for naming raw data.
library(tidyverse)
library(lubridate)
em_price <- read.csv("ptf-smf.csv")
glimpse(em_price)
## Rows: 744
## Columns: 6
## $ Date <chr> "01.07.20 00:00", "01.07.20 01:00",…
## $ MCP. <dbl> 323.85, 326.95, 324.31, 322.11, 320…
## $ SMP. <dbl> 211.00, 201.00, 211.00, 211.00, 201…
## $ Positive.Imbalance.Price..TL.MWh. <dbl> 204.67, 194.97, 204.67, 204.67, 194…
## $ Negative.Imbalance.Price..TL.MWh. <dbl> 333.57, 336.76, 334.04, 331.77, 329…
## $ SMP.Direction <chr> "?Energy Surplus", "?Energy Surplus…
Sys.setenv(TZ="Europe/London") # for date-time adjustments.
As you can see here, data have 6 variables two of them are “characters” while others are numeric. Since Date column in the dataframe is not a date time object, it is not appropriate to use it with filter function and other date calculations. In order to convert that we use;
em_price$Date <- parse_date_time(em_price$Date, orders = c( "dmyHM", "mdyHM"))
Now it is a date-time object;
class(em_price$Date)
## [1] "POSIXct" "POSIXt"
I would like to use the weekdays as “Monday”, “Tuesday”, “Wednesday” etc. so create another column that holds the days of week;
em_price <- em_price %>% mutate(day_name = weekdays(as.Date(em_price$Date)))
The data can be organized in a way that a person who is not familiar the concept of electricity pricing can easily understand. One might wonder what is the average price according to days and because there is a correlation between electricity prices and usage she might want to know difference between weekdays and the weekend. For that, we grouping the data and taking means of hourly prices by days.
em_price_dh2 <- em_price %>% select(Date, MCP., SMP., day_name) %>%
group_by(hour = lubridate ::hour(Date), day_name) %>% summarise(mcp_hrly_price = mean(MCP.))
em_price_dh2
## # A tibble: 168 x 3
## # Groups: hour [24]
## hour day_name mcp_hrly_price
## <int> <chr> <dbl>
## 1 0 Friday 292.
## 2 0 Monday 288.
## 3 0 Saturday 294.
## 4 0 Sunday 296.
## 5 0 Thursday 302.
## 6 0 Tuesday 302.
## 7 0 Wednesday 306.
## 8 1 Friday 305.
## 9 1 Monday 305.
## 10 1 Saturday 305.
## # … with 158 more rows
In this graph, tracking of changes in price is available;
graph_1<-ggplot(em_price_dh2, aes(x=hour, y=mcp_hrly_price, group=day_name)) +
geom_line(aes(color=day_name))+
geom_point(aes(color=day_name))
graph_1 <- graph_1 + labs(x="Hours of the Day", y="Price", color="Days", title = "MCP Hourly Averages by Days", subtitle = " 1-31 July 2020")
graph_1
Sunday is clearly out of trend. But suprisingly, Saturday is not distinguished from other days since most firms in Turkey works on Saturday.
Another column in dataframe is “SMP.Direction” which indicates balance of demand and supply. From that information, a seller firm can calculate its losses(cost of wrong prediction). A simple formulation can be implemented to dataframe to extract the loss: when energy-surplus occur firm sells electricity from lower price, on the contrary energy-deficit requires to buy electricity from higher price. In-balance situation, firms sells or buys electricity from right price but its an ideal case and rarely occurs. Piece of code:
unique(em_price$SMP.Direction) # which components exist?
## [1] "?Energy Surplus" "? Energy Deficit" "? In Balance"
Now we are creating new column “Unit_Loss”.
graph_df <- em_price %>% transmute(Date, MCP. , SMP., unit_loss = case_when(SMP.Direction == "?Energy Surplus" ~ (MCP. - Positive.Imbalance.Price..TL.MWh.),
SMP.Direction == "? Energy Deficit" ~ (Negative.Imbalance.Price..TL.MWh. - MCP.), SMP.Direction == "? In Balance" ~ 0 ))
graph_2<- graph_df %>% pivot_longer(.,-Date) %>% ggplot(., aes(x=Date, y=value, color=name)) + geom_line()
graph_2 <- graph_2 + labs(x="Time Line", y="Price", color="Variables", title = "Price Changes and Unit Loss", subtitle = " 1-31 July 2020")
graph_2
To see clearly, we can select one-week period:
graph_3<- graph_df %>% filter(Date >= "2020-07-01" & Date <= "2020-07-07")%>% pivot_longer(.,-Date) %>% ggplot(., aes(x=Date, y=value, color=name)) + geom_line()
graph_3 <- graph_3 + labs(x="Time Line", y="Price", color="Variables", title = "Price Changes and Unit Loss", subtitle = " 1-7 July 2020")
graph_3
Another question can be “what is the percentage of Balance_State?” :
gdata_percentage <- data.frame(em_price$SMP.Direction)
pie_chart <- ggplot(gdata_percentage) + geom_bar(mapping = aes(x = "", fill=em_price.SMP.Direction ), width = 1) + coord_polar("y") +
theme_void() # remove background, grid, numeric labels
pie_chart <- pie_chart + labs(x="Balance State", color="Balance State", title = "Percentage of State", subtitle = " 1-31 July 2020")
pie_chart
We might compare the hourly values of SMP and MCP with the help of scatter plot. First we make some arrangements in the data:
graph_df1 <- em_price %>% select(Date, MCP., SMP., day_name) %>%
group_by(hour = lubridate ::hour(Date), day_name) %>% summarise(mcp_hrly_price = mean(MCP.), smp_hrly_price = mean(SMP.))
graph_df1 <- data.frame(graph_df1)
sc_plot <- ggplot(graph_df1) + geom_point(aes(x = mcp_hrly_price, y = smp_hrly_price, color=as.character(hour)))
sc_plot <- sc_plot + labs(x="Hourly MCP Price", y="Hourly SMP Price", color="Hours", title = "MCP vs SMP Hourly averages")
sc_plot