In the first place, little R has done a lot of tutoring since she came into contact with R language, and she thinks she can share her accumulated experience with everyone for free. Therefore, she made the tutoring notes into an article, thanks for your support. If there is any doubt in the drawing, you can directly contact xiaobian or pay attention to the public number algorithm learning schedule. (Ps because the content is original, so prohibit all kinds of commercial ha, reproduced to indicate the source ha)
Article published in: Algorithmic Learning Calendar
Ggfortify, a powerful graphing package released in July 2021, is based on GGploT2 and uses uniform autoplot functions and simple parameters to graphing all basic models in a straightforward way.
Xiaxiaian has written two nanny level tutorial articles to introduce how to do Kmeans clustering analysis, survival analysis, linear regression model drawing, interested friends or zero basic beginners directly click on the following article to view ha.
【 Drawing tutorial 】3 minutes to learn R language drawing
Learning to draw in R in 3 minutes (2)
This section builds on the previous article, with specific steps for the graphical output of GgFortify in terms of the time series model. (Ps because the content is original, so prohibit all kinds of commercial ha, reproduced to indicate the source ha)
1. Trend diagram of time series
Autoplot supports time series data converted by different functions in a time series plot, and the graph is the same.
The specific operation code is as follows:
library(zoo)autoplot(AirPassengers)autoplot(xts::as.xts(AirPassengers))autoplot(timeSeries::as.timeSeries(AirPassengers) )Copy the code
The figure doesn’t look very nice, and autoplot sets the tuning parameters for multiple aspects of ts data. For your convenience, here is a table:
ts.geom | Display time series data trends in the form of dots, lines, bars and jagged graphs. Parameter values can be selected from ‘line’, ‘bar’, ‘ribbon’, or ‘point’ |
---|---|
ts.colour | The color of the line |
ts.size | The size of the point |
ts.linetype | The shape of the line |
ts.alpha | alpha for time-series |
ts.fill | Other forms of color configuration other than lines |
ts.shape | The shape of the point |
The specific operation code is as follows:
autoplot(AirPassengers, ts.geom = 'point',ts.fill = 'orange',ts.linetype = 2,ts.shape = 5)
Copy the code
If multiple time series data represent a trend together, setting Facets can determine whether to present it in a split screen or in the same graph. You can also set whether or not the data is standardized while drawing, using the scale parameter.
The specific operation code is as follows:
autoplot(Canada)autoplot(Canada,ts.scale = TRUE)
Copy the code
The specific operation code is as follows:
autoplot(Canada, facets = FALSE,ts.scale = TRUE)
Copy the code
Periodic decomposition can also be done
However, xiaobian found a bug, that is, can not change the color, ha ha, has always been black, it seems that the new package or updated.
The specific operation code is as follows:
model = stats::decompose(UKgas)autoplot(model,fill="red")
Copy the code
2. HoltWinters model
You can plot predictions, you can plot future trends, and you can add confidence intervals.
(1) Simple model prediction result graph, where the green is the prediction curve and the red is the real curve, which is also a very common graph. The specific operation code is as follows:
d.holt <- stats::HoltWinters(USAccDeaths)autoplot(d.holt,colour = 'green')
Copy the code
(2) Plus predict is the display of prediction results of future time, while prediction. Interval controls whether confidence interval appears.
The specific operation code is as follows:
autoplot(d.holt, predict = predict(d.holt, n.ahead = 5, prediction.interval = TRUE))
Copy the code
3. AR, ARMIRA model
There’s a little bit of a problem with the AR, THE ARIMA model prediction graph, which is…. Xiaobian found that there is no prediction map ~, the follow-up find how to solve will update this part.But the ARIFIMA model does. It’s amazing.
The specific operation code is as follows:
Data = XTS ::as.xts(AirPassengers)autoplot(forecast::Arima(data,order = c(1,0,1))Copy the code
The specific operation code is as follows:
autoplot(forecast::arfima(AirPassengers))
Copy the code
4. The VAR model
The drawing of this model is very normal, we will choose the basic black drawing
Here is. Date is changed directly to date data, which is also convenient.
The specific operation code is as follows:
data(Canada, package = 'vars')
d.var <- vars::VAR(Canada, p = 3, type = 'const')
autoplot(stats::predict(d.var, n.ahead = 50), is.date = TRUE)
Copy the code
Of course I can also not show the confidence interval pull ~
autoplot(stats::predict(d.var, n.ahead = 50), conf.int = FALSE)
Copy the code
5. ACF PACF figure
If plot selects false, the exact value of ACf is displayed.
air.acf <- acf(AirPassengers, plot = TRUE)
Copy the code
END
If you have any questions about other R languages, you can send them to xiaobian directly.
More knowledge sharing or tutoring courses will be launched in the future. It is not easy to create. Please click to follow and collect if you are interested.
Zhihu | R language data school