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.823954712 0.074947320 0.568878744 #> [2,] 0.671560953 -2.250901330 -2.102626810 #> [3,] -0.136811085 1.500961160 -0.003301496 #> [4,] -0.324855419 -0.482768943 0.416918015 #> [5,] 1.165364420 0.605210639 0.473867599 #> [6,] -0.903741440 0.800685483 -0.062925119 #> [7,] 0.985612115 0.318362191 -1.636219349 #> [8,] -0.126323527 0.103472107 1.168229652 #> [9,] -1.443967173 -0.657636600 -0.408686195 #> [10,] 0.032154077 0.033874701 -0.094117059 #> [11,] -2.117716447 -0.649756559 0.467589309 #> [12,] 1.665816928 0.911239650 -0.828195377 #> [13,] 0.575671055 48.000000000 0.396813229 #> [14,] -0.002235961 -1.178515013 0.244475509 #> [15,] -0.722014992 2.242660967 0.627437586 #> [16,] 0.448672361 1.467485607 -0.805206472 #> [17,] 0.051736240 0.552061735 -1.932053070 #> [18,] -1.253649454 0.359622515 0.717082011 #> [19,] 0.558374959 1.837521669 1.792486559 #> [20,] 0.298702402 0.883273304 1.123194865 #> [21,] -0.415528828 -1.713201462 1.609947684 #> [22,] -1.625374469 0.293728600 1.732799496 #> [23,] 0.299647475 -62.000000000 1.275703717 #> [24,] -0.146007948 0.001025496 0.424007988 #> [25,] 0.330470436 0.944012124 -0.137778131 #> [26,] -0.344358479 -0.163110155 -0.597937324 #> [27,] 0.891762344 0.020936334 75.000000000 #> [28,] -0.136294335 0.551059233 0.072762483 #> [29,] 0.281191185 0.754814726 1.473362511 #> [30,] -0.110812460 -1.017904646 -0.316206077 #> [31,] -0.036247432 -0.320525550 -1.414721408 #> [32,] 0.024977216 -0.457239570 -1.986179040 #> [33,] 0.131053564 -2.168675688 0.705734028 #> [34,] 2.595523922 0.066409650 -0.790427631 #> [35,] 0.419283992 1.513114967 0.758480158 #> [36,] -0.281698421 33.000000000 2.078375621 #> [37,] 0.544863114 0.196097401 1.919134900 #> [38,] 0.868230689 -0.656023781 0.075529822 #> [39,] -0.206675268 -0.478355050 0.267751527
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,] 0 0 0 #> [12,] 1 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,] 0 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,] 1 0 0 #> [36,] 0 0 0 #> [37,] 0 0 0 #> [38,] 0 0 0 #> [39,] 0 0 0 #> [40,] 0 0 0