This function returns a matrix with columns that have a 1 at indices where the residuals have an outlier, and a 0 everywhere else. Outliers are calculated per variable (column) separately. We consider residual outliers the rows in the column of residuals or in the column of squared residuals that are more than 2.5 times the standard deviation away from the mean (standard deviation and mean are calculated separately per column and for residuals/squared residuals). The dummy columns are prepended with zeros to match the size of the other input variables to the model.

residual_outliers(resid_matrix, number_of_rows)

Arguments

resid_matrix

A matrix of residuals. Column names are copied over to the returned result.

number_of_rows

The number of measurements that were input to the model. Since the length of the residual matrix is shorter depending on the amount of lags in the model, we use number_of_rows to specify the number of rows in the returned matrix.

Value

A matrix with dummy variables in columns following the procedure described above.

Examples

resid_matrix <- matrix(rnorm(39 * 3), nrow = 39, ncol = 3, dimnames = list(NULL, c('rumination', 'happiness', 'activity'))) resid_matrix[13, 2] <- 48 resid_matrix[23, 2] <- -62 resid_matrix[36, 2] <- 33 resid_matrix[27, 3] <- 75 resid_matrix
#> rumination happiness activity #> [1,] 0.43729683 0.08579796 -0.75335821 #> [2,] -0.22108256 0.45080047 -0.45277428 #> [3,] 1.57990516 -1.26579132 -0.48036755 #> [4,] -0.15324576 -0.60765079 0.49909522 #> [5,] 1.76480495 -1.18114509 1.16439403 #> [6,] -0.34681482 -0.91630136 1.24340573 #> [7,] -0.29802957 0.02131316 -0.51719191 #> [8,] -1.21865284 0.22965243 1.27589406 #> [9,] 0.89165313 0.70417970 -0.65252554 #> [10,] 2.26602322 -0.45783602 0.26232134 #> [11,] 2.16586661 0.22136063 -1.53943519 #> [12,] -0.47577340 0.52431959 -1.73269734 #> [13,] -1.37791928 48.00000000 -1.23235358 #> [14,] 0.53984091 -0.43062101 -0.58489202 #> [15,] 1.28709094 -1.38946022 -0.22668289 #> [16,] -0.27074332 0.03268453 0.96864208 #> [17,] 0.24853925 0.67925250 -0.95241061 #> [18,] 1.12569307 -0.88345899 -0.09681659 #> [19,] 1.90115865 1.26483961 0.47353111 #> [20,] 0.20643145 -0.93532043 1.82111087 #> [21,] 0.96786261 0.26032007 0.06192266 #> [22,] -0.55158299 0.84053893 -0.21443954 #> [23,] -0.54894473 -62.00000000 -0.20659308 #> [24,] 2.21567122 -1.75721029 -0.29355459 #> [25,] -0.04737050 0.86228089 -0.73656978 #> [26,] 0.63870933 0.08087304 -2.49893211 #> [27,] 0.12761015 -0.61557102 75.00000000 #> [28,] -1.15695898 -0.38386460 0.32503940 #> [29,] -0.95223082 0.40296930 -0.57929642 #> [30,] -1.64214637 0.36581189 0.81110909 #> [31,] 1.13462761 1.41961913 -1.69736060 #> [32,] 0.27714954 -0.61211131 0.21923792 #> [33,] 0.80854385 2.32575996 0.31340026 #> [34,] 0.04093482 -0.11197165 0.33212261 #> [35,] 0.03564389 -0.41437805 -0.41442555 #> [36,] -0.43662344 33.00000000 -0.31336763 #> [37,] 0.60915631 1.40740789 -0.45631662 #> [38,] -0.64934074 -1.51664401 -0.93402929 #> [39,] -0.34111752 -0.56559156 0.28561748
autovarCore:::residual_outliers(resid_matrix, 40)
#> rumination happiness activity #> [1,] 0 0 0 #> [2,] 0 0 0 #> [3,] 0 0 0 #> [4,] 0 0 0 #> [5,] 0 0 0 #> [6,] 0 0 0 #> [7,] 0 0 0 #> [8,] 0 0 0 #> [9,] 0 0 0 #> [10,] 0 0 0 #> [11,] 1 0 0 #> [12,] 0 0 0 #> [13,] 0 0 0 #> [14,] 0 1 0 #> [15,] 0 0 0 #> [16,] 0 0 0 #> [17,] 0 0 0 #> [18,] 0 0 0 #> [19,] 0 0 0 #> [20,] 0 0 0 #> [21,] 0 0 0 #> [22,] 0 0 0 #> [23,] 0 0 0 #> [24,] 0 1 0 #> [25,] 1 0 0 #> [26,] 0 0 0 #> [27,] 0 0 0 #> [28,] 0 0 1 #> [29,] 0 0 0 #> [30,] 0 0 0 #> [31,] 0 0 0 #> [32,] 0 0 0 #> [33,] 0 0 0 #> [34,] 0 0 0 #> [35,] 0 0 0 #> [36,] 0 0 0 #> [37,] 0 0 0 #> [38,] 0 0 0 #> [39,] 0 0 0 #> [40,] 0 0 0