[Introduction to element decomposition] Let's arrange time series analysis methods in R and python ♬

This time, I further pursued the previous time series decomposition. That is, the model is as follows Y_t=T_t + S_t + e_t Or Y_t=T_tS_te_t It is a decompose () method that decomposes time series data. Last time, each data was drawn at the same time, but this time, for the purpose of reproducing and using pure voice and vibration with noise removed, I tried to save and reproduce each data. The data are the preliminary figures of the number of deaths on the site of reference ③ and the past data of reference ④ up to 2018. The reference this time is as follows. Reference (1) is for R language, and reference (2) is for python. Here, we will summarize the process of digitizing independently and saving it in a csv file. 【reference】 ①Extracting Seasonality and Trend from Data: Decomposition Using RTIME SERIES DECOMPOSITION & PREDICTION IN PYTHON ③ [Japan e-Stat in Statistics is an official statistics portal site where you can browse Japanese statistics](https://www.e-stat.go.jp/stat-search/files?page=1&layout=datalist&toukei= 00450011 & tstat = 000001028897 & cycle = 1 & tclass1 = 000001053058 & tclass2 = 000001053059 & result_back = 1) ④ [Dataset list (5-4 Annual deaths and mortality rates by month of death (per 1,000 population)) @ Japan by statistics](https://www.e-stat.go.jp/stat- search / files? page = 1 & layout = datalist & toukei = 00450011 & tstat = 000001028897 & cycle = 7 & year = 20180 & month = 0 & tclass1 = 000001053058 & tclass2 = 000001053061 & tclass3 = 00001053065 & result_back = 1) The code and data for this time are placed below. ・ MuAuan / R_Audio

What i did

・ Decompose in R ・ Decompose in python

・ Decompose in R

Almost the same as last time, you can specify seasonal data with the notation decompose_IIP $ seasonal.

data <- read.csv("./industrial/sibou_p.csv",encoding = "utf-8")
IIP <- ts(data,start=c(2008),frequency=12)
w_list <- c("seasonal","random","trend","observed")
decompose_IIP <- decompose(IIP)
for (i in w_list){
  if (i=="seasonal"){
    plot(decompose_IIP$seasonal)
  }else if(i=="random"){
    plot(decompose_IIP$random)
  }else if(i=="trend"){
    plot(decompose_IIP$trend)
  }else if(i=="observed"){
    plot(IIP)
  }
}
seasonal random
IIP_compose_sibou_seasonal.png IIP_compose_sibou_random.png
trend observed
IIP_compose_sibou_trend.png IIP_compose_sibou_observed.png

Another code First, draw the original data. Next, since this data has a 12-month cycle, we will take a 12 moving average as a trend curve. Then, superimpose it on the above original data and draw lines.

#install.packages("forecast")
library(forecast)
plot(as.ts(IIP))

trend_beer = ma(IIP, order = 12, centre = T) #4
lines(trend_beer)

plot_line_original.png

plot only draws that trend curve.

plot(as.ts(trend_beer))

trend.png Plot by subtracting the trend data from the original data.

detrend_beer = IIP - trend_beer
plot(as.ts(detrend_beer))

detrend.png Then, the data with the noise minus the trend is divided into 12 pieces of data, and the average of them is calculated as the seasonal variation. 13 of them are drawn in a row. 【reference】 -Row and Column SummariesMatrix

m_beer = t(matrix(data = detrend_beer, nrow = 12))
seasonal_beer = colMeans(m_beer, na.rm = T)
plot(as.ts(rep(seasonal_beer,13)))

seasonal_bear13.png Finally, find the random variation. The result was in agreement with what we found with the decompose () function. ** I'm looking for this calculation **.

random_beer = IIP - trend_beer - seasonal_beer
plot(as.ts(random_beer))

calc_random.png Finally, for various uses, output to a csv file as follows.

write.csv(as.ts(rep(seasonal_beer,1300)), file = 'decompose_seasonal1300.csv')

・ Decompose in python

Similar functions are available in python. The code is below.

import pandas as pd
import statsmodels.api as sm
from statsmodels.tsa.seasonal import STL
import matplotlib.pyplot as plt

# Set figure width to 12 and height to 9
plt.rcParams['figure.figsize'] = [12, 9]
df = pd.read_csv('sibou_.csv')
series = df['Price']
print(series)

cycle, trend = sm.tsa.filters.hpfilter(series, 144)
fig, ax = plt.subplots(3,1)
ax[0].plot(series)
ax[0].set_title('Price')
ax[1].plot(trend)
ax[1].set_title('Trend')
ax[2].plot(cycle)
ax[2].set_title('Cycle')
plt.show()

Figure_1_py.png

Another way with python Personally, if the result of decomposition is the above method, another process is required for cycle, and this seems to be more reasonable because it can process even random parts. Also, compared to R, seasonal seems to have a larger vibration width year by year (although it is natural that R disappears because this fluctuation is averaged). In some cases, this method is the most versatile. 【reference】 -Seasonal-Trend decomposition using LOESS (STL)

stl=STL(series, period=12, robust=True)
result = stl.fit()
chart = result.plot()
chart1= result.plot(observed=False, resid=False)
chart2= result.plot(trend=False, resid=False)
plt.show()

Observed & Trend & Seasonal & Residual Figure_2_py.png Trend & Seasonal Figure_3_py.png Obeserved & Seasonal Figure_4_py.png Also, if you do the following, you can output to one graph as a separate graph.

pl1 = result.observed
pl2 = result.trend
pl3 = result.seasonal
pl4 = result.resid

plt.plot(pl1)
plt.plot(pl2)
plt.plot(pl3)
plt.plot(pl4)
plt.show()
plt.close()

pl1-4.png

You can easily output by doing the following.

pl5 = result.seasonal.plot()
plt.show()
plt.close()
...
seasonal resid
seasonal.png resid.png
trend observed
trend.png observed.png

And the csv file output is done as follows.

import csv
with open('sample_pl2.csv', 'w') as f:
    writer = csv.writer(f, delimiter='\n')
    writer.writerow(pl2)

Summary

・ I tried to arrange the method of decompose () with R and python ・ The results of each code are slightly different, but they can be almost the same, and the periodic fluctuations with noise and trends can be extracted as pure periodic fluctuations. ・ Each element decomposed component can be written to csv data and can be used secondarily. ・ Actually, this time, taking the secular change of the number of deaths as an example [preliminary figures until March](https://www.e-stat.go.jp/stat-search/files?page=1&layout=datalist&toukei=00450011&tstat=000001028897&cycle = 1 & tclass1 = 000001053058 & tclass2 = 000001053059 & result_back = 1), but no abnormal value like 2011 is seen in that range (rather, it is decreasing)

・ Next, let's convert the periodic fluctuation element into sound and listen to it. ・ Try for aperiodic fluctuation elements (aside from the test)

Recommended Posts

[Introduction to element decomposition] Let's arrange time series analysis methods in R and python ♬
Introduction to Time Series Analysis ~ Seasonal Adjustment Model ~ Implemented in R and Python
[Introduction to Sound] Let's arrange the introduction to sounds of python and R ♬-Listen to the sound of the explosion of Nikkei 255-
"Introduction to data analysis by Bayesian statistical modeling starting with R and Stan" implemented in Python
To represent date, time, time, and seconds in Python
Convert timezoned date and time to Unixtime in Python2.7
Introduction to Linear Algebra in Python: A = LU Decomposition
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
How to generate exponential pulse time series data in python
Graph time series data in Python using pandas and matplotlib
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
An introduction to statistical modeling for data analysis (Midorimoto) reading notes (in Python and Stan)
Determine the date and time format in Python and convert to Unixtime
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
[Introduction to Python3 Day 1] Programming and Python
Python: Time Series Analysis: Preprocessing Time Series Data
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
[Introduction to Python] Let's use pandas
Hashing data in R and Python
[Introduction to Python] Let's use pandas
Introduction to image analysis opencv python
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
[Introduction to Python] Let's use pandas
Power of forecasting methods in time series data analysis Semi-optimization (SARIMA) [Memo]
How to get the date and time difference in seconds with python
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python
How to stop a program in python until a specific date and time
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Python] How to use class in Python?
Bind methods to Python classes and instances
[Introduction to Python] Let's use foreach with Python
Private methods and fields in python [encryption]
Easy introduction of python3 series and OpenCV3
Adding Series to columns in python pandas
How to use is and == in Python
Time series analysis # 6 Spurious regression and cointegration
Introduction to Vectors: Linear Algebra in Python <1>
Introduction to Effectiveness Verification Chapter 1 in Python
Python: Time Series Analysis: Building a SARIMA Model
Get time series data from k-db.com in Python
Introduction to Python Let's prepare the development environment
How to generate permutations in Python and C ++
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
Python: Time Series Analysis: Stationarity, ARMA / ARIMA Model
tse --Introduction to Text Stream Editor in Python
How to use Python Image Library in python3 series
I wrote "Introduction to Effect Verification" in Python
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
A clever way to time processing in Python
Send messages to Skype and Chatwork in Python
Survival time analysis learned in Python 2 -Kaplan-Meier estimator
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
Smoothing of time series and waveform data 3 methods (smoothing)
How to read time series data in PyTorch
Introduction to Effectiveness Verification Chapter 2 Written in Python
How to plot autocorrelation and partial autocorrelation in python
Reading, summarizing, visualizing, and exporting time series data to an Excel file with Python