how to find sum of square for treatment from array in matlab

To find the sum of squares for treatment from an array in MATLAB, you can use the "anova1" function from the Statistics and Machine Learning Toolbox.

First, let's create a sample data array:

main.m
data = [10 12 14; 8 9 11; 6 7 9; 11 12 13]
43 chars
2 lines

This creates a 4x3 matrix with four samples and three treatments.

Next, we can use the "anova1" function to perform a one-way ANOVA on the data:

main.m
[p,tbl,stats] = anova1(data,[],'off');
39 chars
2 lines

The "anova1" function returns three outputs:

  • p: The p-value of the ANOVA test
  • tbl: A table containing the ANOVA results
  • stats: A structure containing additional statistics

By setting the second input to "[]" and the third input to "'off'", we prevent the "anova1" function from creating graphical output.

The sum of squares for treatment can be found in the ANOVA table:

main.m
SS_trt = tbl{2,2}
18 chars
2 lines

This retrieves the value in the second row and second column of the ANOVA table, which corresponds to the sum of squares for treatment.

Finally, we can check our answer by calculating the sum of squares for error and verifying that the total sum of squares equals the sum of squares for treatment plus the sum of squares for error:

main.m
SS_err = sum(sum((data - repmat(mean(data),4,1)).^2));
SS_total = SS_trt + SS_err;
83 chars
3 lines

If the ANOVA was performed correctly, SS_total should equal the total sum of squares, which is calculated as:

main.m
SS_total = sum(sum((data - mean(mean(data))).^2));
51 chars
2 lines

related categories

gistlibby LogSnag