Title: | Generalized Space-Time Autoregressive Model |
---|---|
Description: | A package for analyzing multivariate time series data using method Generalized Space-Time Autoregressive Model by Ruchjana et al(2012) <doi:10.1063/1.4724118>. |
Authors: | Ahmad Zaenal [aut, cre], Fiqry Revadiansyah [aut] |
Maintainer: | Ahmad Zaenal <[email protected]> |
License: | GPL-2 | GPL-3 |
Version: | 0.1.0 |
Built: | 2025-01-23 04:10:00 UTC |
Source: | https://github.com/zaenalium/gstar |
gstar function return the paramter estimatation of Generalized Space-Time Autoregressive Model.
gstar(x, weight, p = 1, d = 0, est = "OLS")
gstar(x, weight, p = 1, d = 0, est = "OLS")
x |
a dataframe, matrix or xts/ts object that contain time series data. |
weight |
a spatial weight ncol(x) * ncol(x) with diagonal = 0. |
p |
an autoregressive order, value must be greater than 0. |
d |
a lag differencing order, value must be greater than 0. |
est |
estimation method, currently only OLS available, another estimation will be added later. |
gstar returns output similar to lm, the detail are shown in the following list :
coefficients - a named vector of coefficients.
AIC - A version of Akaike's An Information Criterion (the calculation similiar with aic in lm method )
Budi Nurani Ruchjana, Svetlana A. Borovkova and H. P. Lopuhaa (2012), Least Squares Estimation of Generalized Space Time AutoRegressive (GSTAR) Model and Its Properties, The 5th International Conference on Research and Education in Mathematics AIP Conf. Proc. 1450, 61-64.
summary
for summarises the model that has been built. Also use predict
to predict model to testing or new data.
library(gstar) library(xts) data("LocationCPI") #-----Use data with xts object----# x = xts(LocationCPI[, -1], order.by = as.Date(LocationCPI[, 1])) s <- round(nrow(x) * 0.8) ## split into training and testing (80:20) x_train <- x[1:s, ] x_test <- x[-c(1:s), ] weight = matrix(c(0, 1, 1, 1, # create the uniform weight. 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0), ncol = 4, nrow = 4) weight = weight/(ncol(x) - 1) #the sum of weight is equal to 1 every row. fit <- gstar(x_train, weight = weight, p = 1, d = 0, est = "OLS") summary(fit) performance(fit) performance(fit, x_test) ## to check the performance with testing data predict(fit, n = 10) #forecast 10 data ahead plot(fit) plot(fit, n_predict = 10) #plot with 10 forecasting data plot(fit, testing = x_test) #---- Use dataframe or matrix---# x2 <- LocationCPI x2$Date <- NULL # remove the date column data(Loc) dst <- as.matrix(dist(Loc[, -1], diag = TRUE, upper = TRUE)) dst1 <- matrix(0, nrow = nrow(dst), ncol = ncol(dst)) for(i in 1:nrow(dst)) { for(j in 1:ncol(dst)){ if(j == i) next dst1[i, j] <- sum(dst[i, -j])/sum(dst[i,]) } } weight_inverse_distance <- matrix(0, nrow = nrow(dst), ncol = ncol(dst)) for(i in 1:nrow(dst)) { for(j in 1:ncol(dst)){ if(j == i) next weight_inverse_distance[i, j] <- sum(dst1[i, j])/sum(dst1[i,]) } } fit_inverse_distance <- gstar(x2, weight = weight_inverse_distance, p = 2, d = 1, est = "OLS") summary(fit_inverse_distance) performance(fit_inverse_distance) predict(fit_inverse_distance) plot(fit_inverse_distance)
library(gstar) library(xts) data("LocationCPI") #-----Use data with xts object----# x = xts(LocationCPI[, -1], order.by = as.Date(LocationCPI[, 1])) s <- round(nrow(x) * 0.8) ## split into training and testing (80:20) x_train <- x[1:s, ] x_test <- x[-c(1:s), ] weight = matrix(c(0, 1, 1, 1, # create the uniform weight. 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0), ncol = 4, nrow = 4) weight = weight/(ncol(x) - 1) #the sum of weight is equal to 1 every row. fit <- gstar(x_train, weight = weight, p = 1, d = 0, est = "OLS") summary(fit) performance(fit) performance(fit, x_test) ## to check the performance with testing data predict(fit, n = 10) #forecast 10 data ahead plot(fit) plot(fit, n_predict = 10) #plot with 10 forecasting data plot(fit, testing = x_test) #---- Use dataframe or matrix---# x2 <- LocationCPI x2$Date <- NULL # remove the date column data(Loc) dst <- as.matrix(dist(Loc[, -1], diag = TRUE, upper = TRUE)) dst1 <- matrix(0, nrow = nrow(dst), ncol = ncol(dst)) for(i in 1:nrow(dst)) { for(j in 1:ncol(dst)){ if(j == i) next dst1[i, j] <- sum(dst[i, -j])/sum(dst[i,]) } } weight_inverse_distance <- matrix(0, nrow = nrow(dst), ncol = ncol(dst)) for(i in 1:nrow(dst)) { for(j in 1:ncol(dst)){ if(j == i) next weight_inverse_distance[i, j] <- sum(dst1[i, j])/sum(dst1[i,]) } } fit_inverse_distance <- gstar(x2, weight = weight_inverse_distance, p = 2, d = 1, est = "OLS") summary(fit_inverse_distance) performance(fit_inverse_distance) predict(fit_inverse_distance) plot(fit_inverse_distance)
A dataset containing the coordinate several region In Indonesia i.e Semarang, Surakarta, Tegal and Purwokerto.
data(Loc)
data(Loc)
A data frame with 4 rows and 3 variables:
Name of region/city
The lattitude coordinate of each location
The longitude coordinate of each location
A dataset containing the Consumer Price Index (CPI) in several region In Indonesia i.e Semarang, Surakarta, Tegal and Purwokerto, it is time series data with monthly periodicity from Jan 2006 to Sep 2014
data(LocationCPI)
data(LocationCPI)
A time series data frame with 105 rows and 5 variables:
date of CPI, monthly
The CPI of Purwokerto region
The CPI of Purwokerto region
The CPI of Purwokerto region
The CPI of Purwokerto region
Calculate performance of prediction or forecasting
performance(object, testing = NULL, ...)
performance(object, testing = NULL, ...)
object |
an object of class "gstar". |
testing |
a dataframe or matrix or xts object that contain testing data (outsample data). Please be noted, if you use differencing in the model estimation, you do not need difference your data because we already cover that in this function |
... |
Other arguments |
MSE fol all data - Mean Square Error for all the data combined
MSE fol each location - Mean Square Error for each spatial location
MAPE fol all data - Mean Absolute Percentage Error for all the data combined
MAPE fol each location - Mean Absolute Percentage Error for each spatial location
plotting the gstar object
## S3 method for class 'gstar' plot(x, testing = NULL, n_predict = NULL, ...)
## S3 method for class 'gstar' plot(x, testing = NULL, n_predict = NULL, ...)
x |
an object of class "gstar". |
testing |
The outsample or testing data to be plotted. |
n_predict |
The number of steps ahead for which prediction is required. |
... |
Other arguments |
Predicted values based on gstar object object
## S3 method for class 'gstar' predict(object, n = NULL, ...)
## S3 method for class 'gstar' predict(object, n = NULL, ...)
object |
an object of class "gstar". |
n |
The number of steps ahead for which prediction is required. |
... |
further arguments passed to or from other methods. |
This function are simmilar to summary of "lm" or "glm" object.
## S3 method for class 'gstar' summary(object, ...)
## S3 method for class 'gstar' summary(object, ...)
object |
an object of class "gstar". |
... |
further arguments passed to or from other methods.
|