Below is the code required to simulate a time series data for an individual with only 1 variable using the simts package. simts package easily allows you to simulate data using specific parameters including AR and MA. However, you can only simulate 1 variable which does not allow for multivariate datasets.
#load required packages
library(simts)
## Warning: package 'simts' was built under R version 4.3.3
#set seed for reproducibility
set.seed(123)
#Create ARIMA model by setting Autoregressive term, differences, moving average term, and standard deviation
ARIMA_model <- ARIMA(ar = 0.5, i = 0, ma = 0.3, sigma2 = 1)
#Create simulated data using ARIMA model
simtsdata <- gen_gts(n = 100, model = ARIMA_model)
#Check first few values of simulated data
head(simtsdata)
## Observed
## [1,] -0.9510100
## [2,] -0.8738252
## [3,] 1.0527425
## [4,] 1.0644921
## [5,] 0.6826863
## [6,] 2.0951945
#Plot simulated time series data
plot(simtsdata, main = "Simulated ARIMA(1,0,1) Time Series")
The forecast package can be used to easily forecast/predict data based on an ARIMA model and then simulated data based on predicted forecasts.
# install required packages
library(forecast)
## Warning: package 'forecast' was built under R version 4.3.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
# We will use the AirPassengers data available from R
data("AirPassengers")
# Plot the data for visual reference
plot(AirPassengers, main = "Monthly Airline Passengers",
xlab = "Year", ylab = "Number of Passengers", col = "blue")
We first fit the existing data to an ARIMA model using auto.arima We can then forecast future values using the forecast function.
# Fit an ARIMA model to the data
fit <- auto.arima(AirPassengers)
summary(fit)
## Series: AirPassengers
## ARIMA(2,1,1)(0,1,0)[12]
##
## Coefficients:
## ar1 ar2 ma1
## 0.5960 0.2143 -0.9819
## s.e. 0.0888 0.0880 0.0292
##
## sigma^2 = 132.3: log likelihood = -504.92
## AIC=1017.85 AICc=1018.17 BIC=1029.35
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 1.3423 10.84619 7.86754 0.420698 2.800458 0.245628 -0.00124847
# Forecast future values and plot forecast
future_forecast <- forecast(fit, h = 52)
plot(future_forecast, main = "Forecast of Monthly Airline Passengers")
Next we use the fitted model to simulate data. As can be seen from the plot below, the simulated data is almost the same as the forecasted data.
# Simulate new data based on forecast model
set.seed(123) # for reproducibility
simulated_data <- simulate(fit, nsim = 52)
# Plot simulated data with added lines colour coded to see how the simulated data compares to the forecast
plot(ts(simulated_data, start = c(1961, 1), frequency = 12), main = "Simulated Future Airline Passengers", xlab = "Year", ylab = "Number of Passengers", col = "red")
lines(AirPassengers, col = "blue") # To show historical data on the same plot
lines(future_forecast$mean, col = "green")
legend("topright", legend = c("Historical", "Simulated", "Forecasted"), col = c("blue", "red", "green"), lty = 1)
Using the MTS package we can generate more than 1 variable’s time series data and manipulate the relationship between generated variables and values. In this example we will simulate a bivariate time series dataset.
# Install and load required packages
library(MTS)
## Warning: package 'MTS' was built under R version 4.3.3
irst we create the phi matrix to create the Autoregressive (AR coefficents). As we are simulating only 2 variables the matrix will be 2x2. The first line are the effects of both variables at t-1 on V1 at time t. The second line are the effects of both variables at t-1 on V2 at time t. So V1 is influenced by its own previous value at time t-1 by a coefficient of 0.3. 0.12 is the coefficent of the influence of V2 at time t-1 on V1 at time t. 0.05 is the coefficient for the effect of V1 at time t-1 on V2 at time 2. And lastly, 0.6 is the AR coefficient for V2, indicating how much V2 at time t is influence by V2 at time t-1.
p1=matrix(c(0.3,0.12,
0.05,0.6),
2,2)
We create a similar matrix for moving average coefficients. This matrix specifies how past error terms influence the current values of V1 and V2. -0.1 coefficent means the impact of the previous error terms for V1 on it’s current value. Negative value suggests that a positive error at time t-1 for V1 would contribute negatively (opposite effect) to V1’s value at time t. 0 means that previous error term for V2 at t-1 does not influence V1’s current value.
th1=matrix(c(-0.1,0,
0,-0.2),
2,2)
We then create a sigma matrix which is a covariance matrix that represents the covariance of innovation(error) terms. The matrix shows how error are correlated with each other and with their individual variances. Variance of each variable 4 is the value of variance of the innovations(errors) for V1. It indicates that the errors affecting V1 have a variance of 4, suggesting a higher variability in the errors impacting this variable. 1 is the variance for V2. Covariance between variables 0.8 represent the covariance between errors of the two variables.
We can check and print eigen values to make sure the matrix is valid. (If all eigen values are above 0)
sig=matrix(c(4,0.8,
0.8,0.7),
2,2)
eigenvalues <- eigen(sig)$values
print(eigenvalues)
## [1] 4.1837121 0.5162879
Finally, we can simulate the data using VARMAsim but simulating data based on a VARMA model. 100 is the number of time points, arlags is the number of AR lags, malags is the number of MA lags, phi, theta, and sigma matrices have already been defined above.
mtssimdata <- VARMAsim(100,arlags=c(1),malags=c(1),phi=p1,theta=th1,sigma=sig)
Create a dataframe of only the data. mtssimdata$noises are the error terms used to generate the series. As we can see from the data there are 100 simulated time points for 2 variables.
zt <- mtssimdata$series
print(zt)
## [,1] [,2]
## [1,] 1.76652687 0.838648386
## [2,] 1.72358947 -0.970700407
## [3,] 2.62982946 -0.799614889
## [4,] 1.32559543 -0.341008879
## [5,] 2.04243129 -0.099691367
## [6,] 1.68322571 0.024432483
## [7,] -1.21484835 0.818883704
## [8,] 1.46052920 2.040251827
## [9,] 1.19125716 2.614997346
## [10,] 4.36135610 2.243151073
## [11,] -0.88398416 1.368606709
## [12,] -0.84517073 0.658041057
## [13,] 3.03554147 0.544416823
## [14,] 1.93955361 0.668064149
## [15,] 0.81386687 0.871885442
## [16,] 2.96447006 1.150961344
## [17,] 2.84935711 1.959978816
## [18,] 2.76044957 1.495330319
## [19,] 4.17654855 1.363063083
## [20,] 1.82909139 2.424950835
## [21,] 2.93511205 1.407692280
## [22,] -0.94538248 -0.218181244
## [23,] -0.09903081 -0.326079356
## [24,] 0.87512482 0.367708711
## [25,] -1.10976953 -0.520858426
## [26,] 1.74249268 0.250236170
## [27,] -2.25390375 0.025090144
## [28,] -3.34871907 -2.242577806
## [29,] -1.07983154 -2.143095557
## [30,] -0.53381393 -1.277686287
## [31,] 1.56881755 -0.385554219
## [32,] -0.80062063 -0.714950115
## [33,] -0.59028982 -0.470878439
## [34,] -2.32855769 -0.757927950
## [35,] 0.93528265 -0.628338019
## [36,] -1.02455542 -0.644544008
## [37,] 1.87923934 0.160982743
## [38,] 3.29589850 0.940117194
## [39,] 1.93480414 0.733367940
## [40,] 1.73357817 0.386017707
## [41,] -2.12965618 0.084493439
## [42,] 3.17046086 0.924925139
## [43,] 3.81547975 1.792769851
## [44,] 0.21242831 1.322413728
## [45,] -0.67283877 0.304941271
## [46,] 0.79269679 -0.723293668
## [47,] 2.27095107 0.443045810
## [48,] -1.28701348 0.879235545
## [49,] 4.08717034 0.670108276
## [50,] 3.17007437 0.576246856
## [51,] 3.39474293 1.169859647
## [52,] 4.15071377 0.545646562
## [53,] 1.30314310 0.263558445
## [54,] -0.19388464 -0.322499870
## [55,] -1.61417505 -0.114081479
## [56,] -2.36094149 0.630611720
## [57,] -3.16720530 0.001098173
## [58,] 0.04265725 0.182652133
## [59,] -0.42974092 0.212752343
## [60,] 1.30502983 -0.793998116
## [61,] -0.95440047 -0.467566580
## [62,] -0.93878988 0.556695152
## [63,] 1.05105195 0.662223438
## [64,] -2.49246564 0.157845182
## [65,] -1.69326183 -0.459498317
## [66,] -2.79044128 -0.463821637
## [67,] -3.20123008 -1.081556260
## [68,] -0.69855958 -1.633400368
## [69,] 0.53422130 -2.029516789
## [70,] -5.28975271 -1.774812808
## [71,] -0.63760068 -1.770412515
## [72,] 0.54133186 -1.896458312
## [73,] -0.67969254 -2.786103188
## [74,] 2.45125688 -0.263492356
## [75,] 3.12728113 0.785833949
## [76,] 0.68445391 -0.295105598
## [77,] 1.64323771 -0.271536560
## [78,] -1.07864908 -1.251592240
## [79,] -1.39597703 -1.893374716
## [80,] -1.73548404 -2.524929294
## [81,] 2.57431089 -1.461465706
## [82,] 2.40693907 -2.203981199
## [83,] -0.34306177 -2.041222954
## [84,] -2.28666727 -0.538065292
## [85,] -3.61875841 -0.584920136
## [86,] 0.34089204 -0.432271239
## [87,] -1.20635343 0.343998022
## [88,] -0.46090906 -0.626737454
## [89,] -2.30952352 0.647866982
## [90,] -2.75455254 -1.294169222
## [91,] 0.06603028 -1.039771812
## [92,] -3.15920697 -2.453438762
## [93,] -1.29495541 -1.373820726
## [94,] 0.66896358 -1.010369414
## [95,] -1.31056634 -0.595174451
## [96,] -0.08650579 0.019762834
## [97,] -0.49854984 0.700882388
## [98,] -1.90661976 -0.020958774
## [99,] 1.00555583 0.650189932
## [100,] -3.29573575 0.232151299
Lastly we can plot the simulated data for both variables.
plot.ts(zt, xlab = "Time", main = "Time Series Data")
We can also generate data using the multi-level vector auto regression simulator which is part of the mlVAR package. First we need to understand how to set the various parameters.
thetaVar represents the variance of random effects and controls how much the individual-specific coefficients can deviate from the population mean. Ie., the heterogeneity in the VAR parameters across individuals.
thetaVar <- c(0.2, 0.5, 0.2) # Low variability in random effects
Higher thetaVar results in higher variability in random effects.
DF_theta controls the degrees of freedom for the multivariate t-distribution from which random effects are sampled. It influences the shape of the distribution of random effects.
DF_theta <- 30 # Higher degrees of freedom, closer to normal distribution
Higher values of DF_theta make the distribution of the random effects closer to a normal distribution.
mu_SD is the standard deviation of the mean of random effects.
mu_SD <- c(1, 2, 1)
init_beta_SD represents the standard deviation for the initial random effects and sets the initial variability of the random effects.
init_beta_SD <- c(0.1, 1, 0.1) # Different initial variability for different variables
fixedMuSD stands for the standard deviation of the fixed effects’ means. It controls how much the fixed effects (the average effects common to all individuals) can vary across the population.
fixedMuSD = 0.2
shrink_fixed and shrink_deviation control control how much the individual-specific estimates are pulled towards the population mean, affecting the extent of individual differences.
shrink_fixed <- 0.3 # Shrinkage for fixed effects
shrink_deviation <- 0.3 # Shrinkage for deviations
We can then simulate the data using the parameters above with 20 individuals, 3 variables (nNode), and 100 time points.
# Simulate the data
library(mlVAR)
## Warning: package 'mlVAR' was built under R version 4.3.3
mlvarsimdata <- mlVARsim(nPerson = 20, nNode = 3, nTime = 100, lag = 1,
thetaVar = thetaVar, DF_theta = DF_theta, mu_SD = mu_SD,
init_beta_SD = init_beta_SD, fixedMuSD = 0.2,
shrink_fixed = shrink_fixed, shrink_deviation = shrink_deviation)
# Check the structure of the simulated data
str(mlvarsimdata)
## List of 5
## $ Data :'data.frame': 2000 obs. of 4 variables:
## ..$ V1: num [1:2000] -2.04 -3.12 -1.36 -3.2 -3.71 ...
## ..$ V2: num [1:2000] 4.47 5.65 3.16 4.9 6.47 ...
## ..$ V3: num [1:2000] 2.89 2.99 1.73 3.28 3.95 ...
## ..$ ID: int [1:2000] 1 1 1 1 1 1 1 1 1 1 ...
## $ vars : chr [1:3] "V1" "V2" "V3"
## $ idvar: chr "ID"
## $ lag : num 1
## $ model:List of 5
## ..$ mu :List of 7
## .. ..$ mean : num [1:3] -0.231 0.464 -0.121
## .. ..$ SD : num [1:3] 1 2 1
## .. ..$ lower : num [1:3(1d)] NA NA NA
## .. ..$ upper : num [1:3(1d)] NA NA NA
## .. ..$ SE : logi [1:3(1d)] NA NA NA
## .. ..$ P : logi [1:3(1d)] NA NA NA
## .. ..$ subject:List of 20
## .. .. ..$ : num [1:3] -2.71 5.17 3.03
## .. .. ..$ : num [1:3] -0.076 0.0391 -0.9462
## .. .. ..$ : num [1:3] -0.414 -1.458 -0.381
## .. .. ..$ : num [1:3] -0.2278 -0.0912 -0.2413
## .. .. ..$ : num [1:3] 1.662 -0.761 -1.337
## .. .. ..$ : num [1:3] 0.0971 -0.0141 0.6694
## .. .. ..$ : num [1:3] -0.795 1.818 0.489
## .. .. ..$ : num [1:3] -1.554 1.057 0.954
## .. .. ..$ : num [1:3] 2.11 -2.32 -1.54
## .. .. ..$ : num [1:3] 1.113 -0.701 -1.313
## .. .. ..$ : num [1:3] -1.482 0.693 0.291
## .. .. ..$ : num [1:3] 2.57 -3.05 -2.93
## .. .. ..$ : num [1:3] -1.73 1 1.37
## .. .. ..$ : num [1:3] -0.437 0.968 0.612
## .. .. ..$ : num [1:3] 2.74 -1.8 -2.48
## .. .. ..$ : num [1:3] -3.59 3.73 2.65
## .. .. ..$ : num [1:3] 0.91 -0.189 -1.431
## .. .. ..$ : num [1:3] 2.064 -0.226 -1.212
## .. .. ..$ : num [1:3] 0.117 0.793 0.739
## .. .. ..$ : num [1:3] -0.431 1.364 -0.043
## .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## ..$ Beta :List of 7
## .. ..$ mean : num [1:3, 1:3, 1] 0.0895 0 0 0.1461 0.0402 ...
## .. ..$ SD : num [1:3, 1:3, 1] 0.022 0.0238 0.0681 0.1431 0.1181 ...
## .. ..$ lower : num [1:3, 1:3, 1] NA NA NA NA NA NA NA NA NA
## .. ..$ upper : num [1:3, 1:3, 1] NA NA NA NA NA NA NA NA NA
## .. ..$ SE : logi [1:3, 1:3, 1] NA NA NA NA NA NA ...
## .. ..$ P : logi [1:3, 1:3, 1] NA NA NA NA NA NA ...
## .. ..$ subject:List of 20
## .. .. ..$ : num [1:3, 1:3, 1] 0.1261 0.0116 0.0315 0.333 0.154 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.1225 0.0382 -0.0734 -0.011 0.1266 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.07114 0.00743 -0.09415 0.12157 0.01413 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.0944 -0.0157 0.0403 -0.0958 0.0154 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.0749 0.0177 0.0573 0.1874 -0.1322 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.09703 -0.00657 0.0758 0.12258 -0.04258 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.0911 0.0219 -0.029 0.0999 -0.0304 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.12278 0.000281 -0.093498 -0.005029 0.172722 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.1154 -0.0146 -0.0119 0.2009 -0.1144 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.08401 0.00647 0.0351 0.20906 -0.02724 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.0812 0.0292 -0.0967 0.3441 0.0711 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.087 -0.031 -0.0841 0.0614 0.0381 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.0665 0.0205 -0.0227 0.1342 -0.0911 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.09204 -0.00977 -0.12269 0.12528 -0.10143 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.0957 -0.0307 0.0659 -0.1138 -0.0814 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.1325 -0.0151 -0.1122 0.2277 0.2262 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.08541 -0.03249 -0.00324 -0.05827 0.13625 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.07892 -0.00452 -0.07706 0.16193 0.24975 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.1007 -0.0192 -0.0486 0.0428 0.0853 ...
## .. .. ..$ : num [1:3, 1:3, 1] 0.0753 -0.04 0.0273 0.0755 -0.0281 ...
## .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## ..$ Omega_mu:List of 4
## .. ..$ cov :List of 6
## .. .. ..$ mean : num [1:3, 1:3] 2.97 -2.88 -2.58 -2.88 3.69 ...
## .. .. ..$ SD : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ lower: num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ upper: num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ SE : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..$ cor :List of 7
## .. .. ..$ mean : num [1:3, 1:3] 1 -0.869 -0.93 -0.869 1 ...
## .. .. ..$ SD : num(0)
## .. .. ..$ lower : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ upper : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ SE : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ subject: list()
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..$ prec:List of 7
## .. .. ..$ mean : num [1:3, 1:3] 2.49 8.44e-15 2.47 5.11e-15 2.15 ...
## .. .. ..$ SD : num(0)
## .. .. ..$ lower : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ upper : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ SE : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ subject: list()
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..$ pcor:List of 7
## .. .. ..$ mean : num [1:3, 1:3] 1.00 -3.65e-15 -6.67e-01 -2.21e-15 1.00 ...
## .. .. ..$ SD : num(0)
## .. .. ..$ lower : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ upper : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ SE : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ subject: list()
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..- attr(*, "class")= chr [1:2] "mlVarCov" "list"
## ..$ Theta :List of 4
## .. ..$ cov :List of 7
## .. .. ..$ mean : num [1:3, 1:3] 0.2 -0.295 -0.186 -0.295 0.5 ...
## .. .. ..$ SD : num [1:3, 1:3] 0.0478 0.0744 0.0438 0.0744 0.1219 ...
## .. .. ..$ lower : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ upper : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ SE : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ subject:List of 20
## .. .. .. ..$ : num [1:3, 1:3] 0.255 -0.382 -0.231 -0.382 0.649 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.116 -0.173 -0.102 -0.173 0.33 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.158 -0.217 -0.16 -0.217 0.34 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.124 -0.129 -0.128 -0.129 0.193 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.118 -0.21 -0.115 -0.21 0.436 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.213 -0.316 -0.195 -0.316 0.558 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.187 -0.317 -0.203 -0.317 0.586 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.0978 -0.1301 -0.0932 -0.1301 0.24 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.276 -0.404 -0.253 -0.404 0.628 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.193 -0.291 -0.168 -0.291 0.49 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.138 -0.237 -0.15 -0.237 0.487 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.214 -0.308 -0.188 -0.308 0.491 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.171 -0.246 -0.18 -0.246 0.399 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.189 -0.285 -0.209 -0.285 0.488 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.189 -0.263 -0.16 -0.263 0.411 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.238 -0.315 -0.205 -0.315 0.494 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.224 -0.357 -0.234 -0.357 0.623 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.181 -0.264 -0.163 -0.264 0.442 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.166 -0.253 -0.146 -0.253 0.498 ...
## .. .. .. ..$ : num [1:3, 1:3] 0.162 -0.232 -0.152 -0.232 0.409 ...
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..$ cor :List of 7
## .. .. ..$ mean : num [1:3, 1:3] 1 -0.933 -0.931 -0.933 1 ...
## .. .. ..$ SD : num [1:3, 1:3] 0 0.0359 0.0342 0.0359 0 ...
## .. .. ..$ lower : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ upper : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ SE : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ subject:List of 20
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.94 -0.941 -0.94 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.884 -0.904 -0.884 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.935 -0.962 -0.935 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.833 -0.9 -0.833 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.924 -0.833 -0.924 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.918 -0.932 -0.918 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.957 -0.952 -0.957 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.849 -0.854 -0.849 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.97 -0.953 -0.97 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.944 -0.913 -0.944 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.914 -0.938 -0.914 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.951 -0.939 -0.951 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.94 -0.949 -0.94 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.937 -0.953 -0.937 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.941 -0.925 -0.941 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.919 -0.91 -0.919 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.954 -0.97 -0.954 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.933 -0.916 -0.933 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.88 -0.918 -0.88 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.899 -0.938 -0.899 1 ...
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..$ prec:List of 7
## .. .. ..$ mean : num [1:3, 1:3] 71.2 22.7 35.2 22.7 15.4 ...
## .. .. ..$ SD : num [1:3, 1:3] 27.35 8.01 16.72 8.01 5.13 ...
## .. .. ..$ lower : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ upper : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ SE : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ subject:List of 20
## .. .. .. ..$ : num [1:3, 1:3] 78.1 24.6 40.9 24.6 13.8 ...
## .. .. .. ..$ : num [1:3, 1:3] 75.7 19.8 42.5 19.8 13.8 ...
## .. .. .. ..$ : num [1:3, 1:3] 132.5 32.9 80 32.9 23.2 ...
## .. .. .. ..$ : num [1:3, 1:3] 54.2 14.2 30.3 14.2 17.3 ...
## .. .. .. ..$ : num [1:3, 1:3] 62.7 24.7 10.8 24.7 17.3 ...
## .. .. .. ..$ : num [1:3, 1:3] 62.5 17.5 34.7 17.5 11.3 ...
## .. .. .. ..$ : num [1:3, 1:3] 119.7 35.7 49.7 35.7 20.4 ...
## .. .. .. ..$ : num [1:3, 1:3] 67.3 21 30.5 21 15 ...
## .. .. .. ..$ : num [1:3, 1:3] 73.9 32.8 24.3 32.8 31.5 ...
## .. .. .. ..$ : num [1:3, 1:3] 73.4 28.2 29.6 28.2 18.8 ...
## .. .. .. ..$ : num [1:3, 1:3] 110.9 25.3 55.7 25.3 12.7 ...
## .. .. .. ..$ : num [1:3, 1:3] 78.3 28.9 36.3 28.9 21.5 ...
## .. .. .. ..$ : num [1:3, 1:3] 132.4 40.8 64.6 40.8 22.7 ...
## .. .. .. ..$ : num [1:3, 1:3] 133.8 37.7 65.2 37.7 18.5 ...
## .. .. .. ..$ : num [1:3, 1:3] 87.8 33.2 43 33.2 21.6 ...
## .. .. .. ..$ : num [1:3, 1:3] 58 21.3 29.8 21.3 13.5 ...
## .. .. .. ..$ : num [1:3, 1:3] 110.8 25.5 63.1 25.5 18 ...
## .. .. .. ..$ : num [1:3, 1:3] 67.5 24 29.7 24 17.4 ...
## .. .. .. ..$ : num [1:3, 1:3] 59.33 13.71 36.6 13.71 8.92 ...
## .. .. .. ..$ : num [1:3, 1:3] 65.7 13.7 42.6 13.7 13.4 ...
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..$ pcor:List of 7
## .. .. ..$ mean : num [1:3, 1:3] 1 -0.685 -0.678 -0.685 1 ...
## .. .. ..$ SD : num [1:3, 1:3] 0 0.0935 0.1305 0.0935 0 ...
## .. .. ..$ lower : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ upper : num [1:3, 1:3] NA NA NA NA NA NA NA NA NA
## .. .. ..$ SE : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:3, 1:3] NA NA NA NA NA NA ...
## .. .. ..$ subject:List of 20
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.75 -0.752 -0.75 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.611 -0.691 -0.611 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.593 -0.789 -0.593 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.465 -0.718 -0.465 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.748 -0.287 -0.748 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.657 -0.724 -0.657 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.722 -0.682 -0.722 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.662 -0.674 -0.662 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.681 -0.401 -0.681 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.76 -0.592 -0.76 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.673 -0.776 -0.673 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.704 -0.61 -0.704 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.745 -0.789 -0.745 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.757 -0.823 -0.757 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.764 -0.689 -0.764 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.759 -0.732 -0.759 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.571 -0.743 -0.571 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.702 -0.609 -0.702 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.596 -0.74 -0.596 1 ...
## .. .. .. ..$ : num [1:3, 1:3] 1 -0.461 -0.713 -0.461 1 ...
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..- attr(*, "class")= chr [1:2] "mlVarCov" "list"
## ..$ Omega :List of 4
## .. ..$ cov :List of 6
## .. .. ..$ mean : num [1:12, 1:12] 2.97 -2.88 -2.58 0 0 ...
## .. .. ..$ SD : logi [1:12, 1:12] NA NA NA NA NA NA ...
## .. .. ..$ lower: num [1:12, 1:12] NA NA NA NA NA NA NA NA NA NA ...
## .. .. ..$ upper: num [1:12, 1:12] NA NA NA NA NA NA NA NA NA NA ...
## .. .. ..$ SE : logi [1:12, 1:12] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:12, 1:12] NA NA NA NA NA NA ...
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..$ cor :List of 7
## .. .. ..$ mean : num [1:12, 1:12] 1 -0.869 -0.93 0 0 ...
## .. .. ..$ SD : num(0)
## .. .. ..$ lower : num [1:12, 1:12] NA NA NA NA NA NA NA NA NA NA ...
## .. .. ..$ upper : num [1:12, 1:12] NA NA NA NA NA NA NA NA NA NA ...
## .. .. ..$ SE : logi [1:12, 1:12] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:12, 1:12] NA NA NA NA NA NA ...
## .. .. ..$ subject: list()
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..$ prec:List of 7
## .. .. ..$ mean : num [1:12, 1:12] 2.49 8.44e-15 2.47 0.00 0.00 ...
## .. .. ..$ SD : num(0)
## .. .. ..$ lower : num [1:12, 1:12] NA NA NA NA NA NA NA NA NA NA ...
## .. .. ..$ upper : num [1:12, 1:12] NA NA NA NA NA NA NA NA NA NA ...
## .. .. ..$ SE : logi [1:12, 1:12] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:12, 1:12] NA NA NA NA NA NA ...
## .. .. ..$ subject: list()
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..$ pcor:List of 7
## .. .. ..$ mean : num [1:12, 1:12] 1.00 -3.65e-15 -6.67e-01 0.00 0.00 ...
## .. .. ..$ SD : num(0)
## .. .. ..$ lower : num [1:12, 1:12] NA NA NA NA NA NA NA NA NA NA ...
## .. .. ..$ upper : num [1:12, 1:12] NA NA NA NA NA NA NA NA NA NA ...
## .. .. ..$ SE : logi [1:12, 1:12] NA NA NA NA NA NA ...
## .. .. ..$ P : logi [1:12, 1:12] NA NA NA NA NA NA ...
## .. .. ..$ subject: list()
## .. .. ..- attr(*, "class")= chr [1:2] "mlVARarray" "list"
## .. ..- attr(*, "class")= chr [1:2] "mlVarCov" "list"
## - attr(*, "class")= chr "mlVARsim"
Next we can fit a VAR model to the simulated data
# Fit a VAR model to the simulated data
mlvar_model <- mlVAR(mlvarsimdata$Data, vars = mlvarsimdata$vars, idvar = mlvarsimdata$idvar, lags = 1)
## 'estimator' argument set to 'lmer'
## 'temporal' argument set to 'correlated'
## 'contemporaneous' argument set to 'correlated'
## Estimating temporal and between-subjects effects
##
|
| | 0%
|
|======================= | 33%
|
|=============================================== | 67%
|
|======================================================================| 100%
## Estimating contemporaneous effects
##
|
| | 0%
|
|======================= | 33%
|
|=============================================== | 67%
|
|======================================================================| 100%
## Computing random effects
##
|
| | 0%
|
|==== | 5%
|
|======= | 10%
|
|========== | 15%
|
|============== | 20%
|
|================== | 25%
|
|===================== | 30%
|
|======================== | 35%
|
|============================ | 40%
|
|================================ | 45%
|
|=================================== | 50%
|
|====================================== | 55%
|
|========================================== | 60%
|
|============================================== | 65%
|
|================================================= | 70%
|
|==================================================== | 75%
|
|======================================================== | 80%
|
|============================================================ | 85%
|
|=============================================================== | 90%
|
|================================================================== | 95%
|
|======================================================================| 100%
# Summarize the fitted model
summary(mlvar_model)
##
## mlVAR estimation completed. Input was:
## - Variables: V1 V2 V3
## - Lags: 1
## - Estimator: lmer
## - Temporal: correlated
##
## Information indices:
## var aic bic
## V1 215.1966 310.2411
## V2 1517.2959 1612.3404
## V3 661.0754 756.1199
##
##
## Temporal effects:
## from to lag fixed SE P ran_SD
## V1 V1 1 0.089 0.073 0.223 0.173
## V1 V2 1 0.026 0.101 0.795 0.249
## V1 V3 1 -0.039 0.085 0.650 0.217
## V2 V1 1 0.139 0.061 0.022 0.212
## V2 V2 1 0.033 0.058 0.570 0.110
## V2 V3 1 -0.051 0.060 0.402 0.187
## V3 V1 1 -0.132 0.051 0.010 0.117
## V3 V2 1 -0.024 0.081 0.765 0.236
## V3 V3 1 0.032 0.055 0.563 0.101
##
##
## Contemporaneous effects (posthoc estimated):
## v1 v2 P 1->2 P 1<-2 pcor ran_SD_pcor cor ran_SD_cor
## V2 V1 0.000 0.00 -0.660 0.057 -0.919 0.034
## V3 V1 0.000 0.00 -0.665 0.091 -0.920 0.040
## V3 V2 0.466 0.24 0.039 0.127 0.852 0.052
##
##
## Between-subject effects:
## v1 v2 P 1->2 P 1<-2 pcor cor
## V2 V1 0.873 0.816 -0.041 -0.766
## V3 V1 0.000 0.000 -0.716 -0.893
## V3 V2 0.001 0.001 0.560 0.846