Skip to content

Commit 95ed61d

Browse files
Fix out of bounds issue for bgmCompare + na.action = "impute".
1 parent 7ceadb8 commit 95ed61d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/gibbs_functions_compare.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ List impute_missing_data_for_anova_model(
140140
const arma::ivec& baseline_category,
141141
bool independent_thresholds
142142
) {
143+
143144
const int num_variables = observations.n_cols;
144145
const int num_missings = missing_data_indices.n_rows;
145146
const int max_num_categories = arma::max(arma::vectorise(num_categories));
@@ -148,6 +149,7 @@ List impute_missing_data_for_anova_model(
148149
double exponent, rest_score, cumsum, u, GroupInteraction;
149150
int score, person, variable, new_observation, old_observation, gr, int_index;
150151

152+
151153
//Impute missing data
152154
for(int missing = 0; missing < num_missings; missing++) {
153155
// Identify the observation to impute
@@ -203,9 +205,9 @@ List impute_missing_data_for_anova_model(
203205
if(is_ordinal_variable[variable] == true) {
204206
arma::imat num_obs_categories_gr = num_obs_categories[gr];
205207
if(old_observation > 0)
206-
num_obs_categories_gr(old_observation, variable)--;
208+
num_obs_categories_gr(old_observation-1, variable)--; //The zero category is omitted from this matrix
207209
if(new_observation > 0)
208-
num_obs_categories_gr(new_observation, variable)++;
210+
num_obs_categories_gr(new_observation-1, variable)++; //The zero category is omitted from this matrix
209211
num_obs_categories[gr] = num_obs_categories_gr;
210212
} else {
211213
arma::imat sufficient_blume_capel_gr = sufficient_blume_capel[gr];
@@ -222,13 +224,15 @@ List impute_missing_data_for_anova_model(
222224

223225
// Update rest scores
224226
for(int vertex = 0; vertex < num_variables; vertex++) {
225-
int_index = pairwise_effect_indices(vertex, variable);
226-
GroupInteraction = pairwise_effects(int_index, 0);
227-
for(int h = 0; h < num_groups - 1; h++) {
228-
GroupInteraction += projection(gr, h) * pairwise_effects(int_index, h + 1);
227+
if(vertex != variable) {
228+
int_index = pairwise_effect_indices(vertex, variable);
229+
GroupInteraction = pairwise_effects(int_index, 0);
230+
for(int h = 0; h < num_groups - 1; h++) {
231+
GroupInteraction += projection(gr, h) * pairwise_effects(int_index, h + 1);
232+
}
233+
residual_matrix(person, vertex) -= old_observation * GroupInteraction;
234+
residual_matrix(person, vertex) += new_observation * GroupInteraction;
229235
}
230-
residual_matrix(person, vertex) -= old_observation * GroupInteraction;
231-
residual_matrix(person, vertex) += new_observation * GroupInteraction;
232236
}
233237
}
234238
}

0 commit comments

Comments
 (0)