Skip to content

Commit 3471769

Browse files
Handle NaN values as group labels (#393)
1 parent 7b7e8ba commit 3471769

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

inst/anova1.m

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@
146146
error ("anova1: GROUP must be a vector with the same number of rows as x.");
147147
endif
148148

149-
## Identify NaN values (if any) and remove them from X along with
150-
## their corresponding values from group vector
151-
nonan = ! isnan (x);
152-
x = x(nonan);
153-
group = group(nonan, :);
154-
155-
## Convert group to indices and separate names
149+
## Convert group to indices and separate names first
156150
[group_id, group_names] = grp2idx (group);
157151
group_id = group_id(:);
152+
x = x(:);
153+
154+
## identify NaN values in x or missing/empty categories in the group and remove them.
155+
valid_data = !isnan (x) & !isnan (group_id);
156+
x = x(valid_data);
157+
group_id = group_id(valid_data);
158158
named = 1;
159159

160160
## Center data to improve accuracy and keep uncentered data for plotting
@@ -360,3 +360,15 @@
360360
%! assert (tbl{2,5}, 15.523192, 1e-6);
361361
%! assert (tbl{2,3}, 2, 0);
362362
%! assert (tbl{2,4}, 7.5786897, 1e-6);
363+
364+
## testing handling of missing values in both data and grouping variables
365+
%!test
366+
%! y = [10; 20; 9999; NaN; 40; 50];
367+
%! g = [1; 1; NaN; 1; 2; 2];
368+
%! [p, tbl, stats] = anova1 (y, g, "off");
369+
%! assert (p, 0.051317, 1e-6);
370+
%! assert (tbl{2,5}, 18, 1e-6);
371+
%! assert (tbl{2,3}, 1, 0);
372+
%! assert (tbl{3,3}, 2, 0);
373+
%! assert (tbl{4,3}, 3, 0);
374+
%! assert (stats.n, [2, 2], 0);

0 commit comments

Comments
 (0)