This function uses Amelia::amelia to impute missing (NA) values in the input data set. This function averages over multiple Amelia imputations to obtain more consistent results. The Amelia imputation model uses all variables of the supplied data_matrix, the first lag of those variables, time, time squared, and day-part dummies.

impute_datamatrix(data_matrix, measurements_per_day, imputation_iterations)

Arguments

data_matrix

The raw, unimputed data matrix.

measurements_per_day

The number of measurements per day. This variable is used for adding day part dummy variables to aid the imputation.

imputation_iterations

The amount of times the Amelia imputation should be averaged over.

Value

This function returns the modified matrix.

Examples

# create a matrix with some missing values data_matrix <- matrix(nrow = 40, ncol = 3) data_matrix[, ] <- runif(ncol(data_matrix) * nrow(data_matrix), 1, nrow(data_matrix)) while (sum(is.na(data_matrix)) == 0) data_matrix[as.logical(round(runif(ncol(data_matrix) * nrow(data_matrix), -0.3, 0.7)))] <- NA colnames(data_matrix) <- c('rumination', 'happiness', 'activity') data_matrix
#> rumination happiness activity #> [1,] 15.074074 29.950721 NA #> [2,] 26.447892 NA 21.075038 #> [3,] NA 25.747227 39.280132 #> [4,] 27.551146 6.328515 14.813282 #> [5,] 34.512150 37.279106 18.777766 #> [6,] 33.706570 26.393627 9.580964 #> [7,] NA 21.619662 NA #> [8,] 10.270212 9.779500 15.400207 #> [9,] 23.566603 39.347408 5.939997 #> [10,] 36.018198 5.252903 29.855028 #> [11,] NA 3.727875 38.278785 #> [12,] 30.673471 31.328175 27.078306 #> [13,] 29.513042 26.674567 19.255485 #> [14,] 34.244863 11.108907 NA #> [15,] 39.056904 NA NA #> [16,] 5.408853 6.799704 25.518602 #> [17,] NA 25.986539 18.649546 #> [18,] 26.275228 8.848299 35.632592 #> [19,] 3.186849 30.001231 31.583871 #> [20,] NA NA 6.384322 #> [21,] 12.490376 34.291799 11.971209 #> [22,] 24.801685 32.333090 28.570913 #> [23,] 5.723246 NA 18.245290 #> [24,] 25.547244 32.019731 27.430242 #> [25,] 28.766257 NA 28.327884 #> [26,] 24.871229 24.401456 3.667850 #> [27,] 2.342740 17.878738 38.381637 #> [28,] 26.969741 36.890756 NA #> [29,] 26.465724 25.024563 NA #> [30,] NA 17.993008 20.607136 #> [31,] NA NA 37.485169 #> [32,] 27.387821 30.991527 18.113186 #> [33,] 35.356391 5.996515 14.668975 #> [34,] 29.517937 22.006692 19.583659 #> [35,] 16.546173 NA 38.680221 #> [36,] 17.790305 NA 12.589315 #> [37,] 6.670887 22.207915 NA #> [38,] 18.593693 10.429268 34.529168 #> [39,] NA 32.322620 8.539194 #> [40,] 3.746268 37.968635 NA
autovarCore:::impute_datamatrix(data_matrix, 1, 100)
#> rumination happiness activity #> [1,] 15.074074 29.950721 22.109948 #> [2,] 26.447892 24.244925 21.075038 #> [3,] 22.296501 25.747227 39.280132 #> [4,] 27.551146 6.328515 14.813282 #> [5,] 34.512150 37.279106 18.777766 #> [6,] 33.706570 26.393627 9.580964 #> [7,] 25.473349 21.619662 22.255266 #> [8,] 10.270212 9.779500 15.400207 #> [9,] 23.566603 39.347408 5.939997 #> [10,] 36.018198 5.252903 29.855028 #> [11,] 25.316632 3.727875 38.278785 #> [12,] 30.673471 31.328175 27.078306 #> [13,] 29.513042 26.674567 19.255485 #> [14,] 34.244863 11.108907 25.738824 #> [15,] 39.056904 19.268480 22.470001 #> [16,] 5.408853 6.799704 25.518602 #> [17,] 27.325148 25.986539 18.649546 #> [18,] 26.275228 8.848299 35.632592 #> [19,] 3.186849 30.001231 31.583871 #> [20,] 28.463178 32.128797 6.384322 #> [21,] 12.490376 34.291799 11.971209 #> [22,] 24.801685 32.333090 28.570913 #> [23,] 5.723246 21.865671 18.245290 #> [24,] 25.547244 32.019731 27.430242 #> [25,] 28.766257 16.279656 28.327884 #> [26,] 24.871229 24.401456 3.667850 #> [27,] 2.342740 17.878738 38.381637 #> [28,] 26.969741 36.890756 13.351239 #> [29,] 26.465724 25.024563 19.173886 #> [30,] 21.167783 17.993008 20.607136 #> [31,] 18.758391 17.310192 37.485169 #> [32,] 27.387821 30.991527 18.113186 #> [33,] 35.356391 5.996515 14.668975 #> [34,] 29.517937 22.006692 19.583659 #> [35,] 16.546173 13.672822 38.680221 #> [36,] 17.790305 26.452100 12.589315 #> [37,] 6.670887 22.207915 25.171412 #> [38,] 18.593693 10.429268 34.529168 #> [39,] 18.295066 32.322620 8.539194 #> [40,] 3.746268 37.968635 18.400788