数学建模社区-数学中国

标题: 求助 [打印本页]

作者: 远行的小船儿666    时间: 2015-10-29 20:51
标题: 求助
这张图,是我在MATLAB中调试这个程序得到的
x=[2.48 2.90 2.5 2.5 2.3    2.39 3.30 2.7 2.8 2.5
    2.62 2.90 2.6 2.9 2.4
    2.51 2.95 2.6 2.2 2.4]
p=anova1(x)
但是,不知道怎样解读,麻烦各位大神,指点迷津!


捕获.PNG (5.32 KB, 下载次数: 275)

捕获.PNG


作者: 森之张卫东    时间: 2015-10-29 22:44
  1. function [p,anovatab,stats] = anova1(x,group,displayopt,extra)
  2. %ANOVA1 One-way analysis of variance (ANOVA).
  3. %   ANOVA1 performs a one-way ANOVA for comparing the means of two or more
  4. %   groups of data. It returns the p-value for the null hypothesis that the
  5. %   means of the groups are equal.
  6. %
  7. %   P = ANOVA1(M) for a matrix M treats each column as a separate group,
  8. %   and determines whether the population means of the columns are equal.
  9. %   This form of ANOVA1 is appropriate when each group has the same number
  10. %   of elements (balanced ANOVA).
  11. %
  12. %   P = ANOVA1(V,GROUP) groups elements in the vector V according to values
  13. %   in the grouping variable GROUP. GROUP must be a categorical variable,
  14. %   numeric vector, logical vector, string array, or cell array of strings
  15. %   with one group name for each element of X.  X values corresponding to
  16. %   the same value of GROUP are placed in the same group.
  17. %
  18. %   P = ANOVA1(M,GROUP) accepts a character array or cell array of strings,
  19. %   with one group name for each column of M. Columns with the same group
  20. %   name are treated as part of the same group.
  21. %
  22. %   P = ANOVA1(X,GROUP,DISPLAYOPT) controls the display. DISPLAYOPT can be
  23. %   'on' (the default) to display figures containing a standard one-way
  24. %   anova table and a boxplot, or 'off' to omit these displays.  Note that
  25. %   the notches in the boxplot provide a test of group medians (see HELP
  26. %   BOXPLOT), and this is not the same as the F test for different means in
  27. %   the anova table. X can be either a vector or matrix. If X is a matrix
  28. %   and there are no group names, specify GROUP as [].
  29. %
  30. %   [P,ANOVATAB] = ANOVA1(...) returns the ANOVA table values as the
  31. %   cell array ANOVATAB.
  32. %
  33. %   [P,ANOVATAB,STATS] = ANOVA1(...) returns an additional structure
  34. %   of statistics useful for performing a multiple comparison of means
  35. %   with the MULTCOMPARE function.
  36. %
  37. %   See also ANOVA2, ANOVAN, BOXPLOT, MANOVA1, MULTCOMPARE.

  38. %   Reference: Robert V. Hogg, and Johannes Ledolter, Engineering Statistics
  39. %   Macmillan 1987 pp. 205-206.

  40. %   Copyright 1993-2013 The MathWorks, Inc.

  41. narginchk(1,4);

  42. classical = 1;
  43. nargs = nargin;
  44. if (nargin>0 && strcmp(x,'kruskalwallis'))
  45.    % Called via kruskalwallis function, adjust inputs
  46.    classical = 0;
  47.    if (nargin >= 2), x = group; group = []; end
  48.    if (nargin >= 3), group = displayopt; displayopt = []; end
  49.    if (nargin >= 4), displayopt = extra; end
  50.    nargs = nargs-1;
  51. end

  52. if (nargs < 2), group = []; end
  53. if (nargs < 3), displayopt = 'on'; end
  54. % Note: for backwards compatibility, accept 'nodisplay' for 'off'
  55. willdisplay = ~(strcmp(displayopt,'nodisplay') | strcmp(displayopt,'n') ...
  56.                 | strcmp(displayopt,'off'));

  57. % Convert group to cell array from character array, make it a column
  58. if (ischar(group) && ~isempty(group))
  59.     group = cellstr(group);
  60. end
  61. if (size(group, 1) == 1)
  62.     group = group';
  63. end

  64. % If the input is a matrix, it may not be balanced if it contains NaNs or
  65. % if there are repeated grouping values, so turn it into a vector.
  66. needvector = false;
  67. if ~isvector(x)
  68.     if (any(isnan(x(:))))
  69.         needvector = true;
  70.     elseif ~isempty(group) && length(group)~=length(unique(group))
  71.         needvector = true;
  72.     end
  73. end

  74. % If X is a matrix with NaNs, convert to vector form.
  75. if ~isvector(x)
  76.    if needvector
  77.       [n,m] = size(x);
  78.       x = x(:);
  79.       gi = reshape(repmat((1:m), n, 1), n*m, 1);
  80.       if isempty(group)     % no group names
  81.          group = gi;
  82.       elseif (size(group,1) == m)
  83.          group = group(gi,:);
  84.       else
  85.          error(message('stats:anova1:InputSizeMismatch'));
  86.       end
  87.    end
  88. elseif ~isempty(group) && (size(group,1) ~= length(x))
  89.    error(message('stats:anova1:InputSizeMismatch'));
  90. end

  91. % If X is a matrix GROUP is provided with the correct size, use GROUP
  92. % to define groups and to label boxes
  93. if (~isempty(group) && (length(x) < numel(x)) ...
  94.                     && (size(x,2) == size(group,1)))
  95.    named = 1;
  96.    [gid,gnames] = grp2idx(group);
  97.    gnames = gnames(gid);
  98.    grouped = 0;
  99. else
  100.    named = 0;
  101.    gnames = [];
  102.    grouped = ~isempty(group);
  103. end

  104. if (grouped)
  105.    % Single data vector and a separate grouping variable
  106.    x = x(:);
  107.    lx = length(x);
  108.    if (lx ~= numel(x))
  109.       error(message('stats:anova1:VectorRequired'))
  110.    end
  111.    nonan = ~isnan(x);
  112.    x = x(nonan);

  113.    % Convert group to indices 1,...,g and separate names
  114.    group = group(nonan,:);
  115.    [groupnum, gnames] = grp2idx(group);
  116.    named = 1;

  117.    % Remove NaN values
  118.    nonan = ~isnan(groupnum);
  119.    if (~all(nonan))
  120.       groupnum = groupnum(nonan);
  121.       x = x(nonan);
  122.    end

  123.    lx = length(x);
  124.    xorig = x;                    % use uncentered version to make M
  125.    groupnum = groupnum(:);
  126.    maxi = size(gnames, 1);
  127.    if isa(x,'single')
  128.       xm = zeros(1,maxi,'single');
  129.    else
  130.       xm = zeros(1,maxi);
  131.    end
  132.    countx = xm;
  133.    if (classical)
  134.       mu = mean(x);
  135.       x = x - mu;                % center to improve accuracy
  136.       xr = x;
  137.    else
  138.       [xr,tieadj] = tiedrank(x);
  139.    end
  140.    
  141.    for j = 1:maxi
  142.       % Get group sizes and means
  143.       k = find(groupnum == j);
  144.       lk = length(k);
  145.       countx(j) = lk;
  146.       xm(j) = mean(xr(k));       % column means
  147.    end

  148.    gm = mean(xr);                      % grand mean
  149.    df1 = sum(countx>0) - 1;            % Column degrees of freedom
  150.    df2 = lx - df1 - 1;                 % Error degrees of freedom
  151.    xc = xm - gm;                       % centered
  152.    xc(countx==0) = 0;
  153.    RSS = dot(countx, xc.^2);           % Regression Sum of Squares
  154. else
  155.    % Data in matrix form, no separate grouping variable
  156.    [r,c] = size(x);
  157.    lx = r * c;
  158.    if (classical)
  159.       xr = x;
  160.       mu = mean(xr(:));
  161.       xr = xr - mu;           % center to improve accuracy
  162.    else
  163.       [xr,tieadj] = tiedrank(x(:));
  164.       xr = reshape(xr, size(x));
  165.    end
  166.    countx = repmat(r, 1, c);
  167.    xorig = x;                 % save uncentered version for boxplot
  168.    xm = mean(xr);             % column means
  169.    gm = mean(xm);             % grand mean
  170.    df1 = c-1;                 % Column degrees of freedom
  171.    df2 = c*(r-1);             % Error degrees of freedom
  172.    RSS = r*(xm - gm)*(xm-gm)';        % Regression Sum of Squares
  173. end

  174. TSS = (xr(:) - gm)'*(xr(:) - gm);  % Total Sum of Squares
  175. SSE = TSS - RSS;                   % Error Sum of Squares

  176. if (df2 > 0)
  177.    mse = SSE/df2;
  178. else
  179.    mse = NaN;
  180. end

  181. if (classical)
  182.    if (SSE~=0)
  183.       F = (RSS/df1) / mse;
  184.       p = fpval(F,df1,df2);        % Probability of F given equal means.
  185.    elseif (RSS==0)                 % Constant Matrix case.
  186.       F = NaN;
  187.       p = NaN;
  188.    else                            % Perfect fit case.
  189.       F = Inf;
  190.       p = 0;
  191.    end
  192. else
  193.    F = (12 * RSS) / (lx * (lx+1));
  194.    if (tieadj > 0)
  195.       F = F / (1 - 2 * tieadj/(lx^3-lx));
  196.    end
  197.    p = chi2pval(F,df1);
  198. end


  199. Table=zeros(3,5);               %Formatting for ANOVA Table printout
  200. Table(:,1)=[ RSS SSE TSS]';
  201. Table(:,2)=[df1 df2 df1+df2]';
  202. Table(:,3)=[ RSS/df1 mse Inf ]';
  203. Table(:,4)=[ F Inf Inf ]';
  204. Table(:,5)=[ p Inf Inf ]';

  205. colheads = {getString(message('stats:anova1:ColHeadSource')), getString(message('stats:anova1:ColHeadSS')), getString(message('stats:anova1:ColHeadDf')), getString(message('stats:anova1:ColHeadMS')), getString(message('stats:anova1:ColHeadF')), getString(message('stats:anova1:ColHeadProbGtF'))};

  206. if (~classical)
  207.    colheads{5} = getString(message('stats:anova1:ColHeadChisq'));
  208.    colheads{6} = getString(message('stats:anova1:ColHeadProbGtChisq'));
  209. end
  210. rowheads = {getString(message('stats:anova1:RowHeadColumns')), getString(message('stats:anova1:RowHeadError')), getString(message('stats:anova1:RowHeadTotal'))};
  211. if (grouped)
  212.    rowheads{1} = getString(message('stats:anova1:RowHeadGroups'));
  213. end

  214. % Create cell array version of table
  215. atab = num2cell(Table);
  216. for i=1:size(atab,1)
  217.    for j=1:size(atab,2)
  218.       if (isinf(atab{i,j}))
  219.          atab{i,j} = [];
  220.       end
  221.    end
  222. end
  223. atab = [rowheads' atab];
  224. atab = [colheads; atab];
  225. if (nargout > 1)
  226.    anovatab = atab;
  227. end

  228. % Create output stats structure if requested, used by MULTCOMPARE
  229. if (nargout > 2)
  230.    if ~isempty(gnames)
  231.       stats.gnames = gnames;
  232.    else
  233.       stats.gnames = strjust(num2str((1:length(xm))'),'left');
  234.    end
  235.    stats.n = countx;
  236.    if (classical)
  237.       stats.source = 'anova1';
  238.       stats.means = xm + mu;
  239.       stats.df = df2;
  240.       stats.s = sqrt(mse);
  241.    else
  242.       stats.source = 'kruskalwallis';
  243.       stats.meanranks = xm;
  244.       stats.sumt = 2 * tieadj;
  245.    end
  246. end

  247. if (~willdisplay), return; end

  248. digits = [-1 -1 0 -1 2 4];
  249. if (classical)
  250.    wtitle = getString(message('stats:anova1:OnewayANOVA'));
  251.    ttitle = getString(message('stats:anova1:ANOVATable'));
  252. else
  253.    wtitle = getString(message('stats:anova1:KruskalWallisOnewayANOVA'));
  254.    ttitle = getString(message('stats:anova1:KruskalWallisANOVATable'));
  255. end
  256. tblfig = statdisptable(atab, wtitle, ttitle, '', digits);
  257. set(tblfig,'tag','table');

  258. f1 = figure('pos',get(gcf,'pos') + [0,-200,0,0],'tag','boxplot');
  259. ax = axes('Parent',f1);
  260. if (~grouped)
  261.    boxplot(ax,xorig,'notch','on');
  262. else
  263.    boxplot(ax,xorig,groupnum,'notch','on');
  264.    h = get(ax,'XLabel');
  265.    set(h,'String',getString(message('stats:anova1:GroupNumber')));
  266. end

  267. % If there are group names, use them
  268. if ~isempty(gnames)
  269.    h = get(ax,'XLabel');
  270.    if (named)
  271.       set(h,'String','');
  272.    end
  273.    set(ax, 'xtick', (1:size(gnames,1)), 'xticklabel', gnames);
  274. end
复制代码


作者: 森之张卫东    时间: 2015-10-29 22:45
在matlab中的ANOVA分为anova1,anova2,anovan共三个函数。
详细说明见官网解释:http://www.mathworks.cn/help/toolbox/stats/anova1.html
anova1: one-way analysis of variance 单因素方差分析
anova2: two-way analysis of variance 两因素方差分析
anovan: n-way analysis of variance 多因素方差分析
一般使用anova1足矣。
特别注意,给出的矩阵X要每个列含有相同个数的元素,做anova时结果会给出p-value,p-value越小,说明各组样品的均值中不全相同,至少有一组的均值有显著性差异。一般的显著性等级(Common significance level)为0.05或者0.01。
若p-value大于Common significance level,则说明各组均值之间没有统计学差异。

作者: 远行的小船儿666    时间: 2015-11-9 18:35
急需大神支招!

作者: 远行的小船儿666    时间: 2015-11-9 18:36



有点难度

作者: 森之张卫东    时间: 2015-11-9 23:02
森之张卫东 发表于 2015-10-29 22:45
在matlab中的ANOVA分为anova1,anova2,anovan共三个函数。
详细说明见官网解释:http://www.mathworks.cn ...

The standard ANOVA table has six columns:
1.        The source of the variability.
2.        The sum of squares (SS) due to each source.
3.        The degrees of freedom (df) associated with each source.
4.        The mean squares (MS) for each source, which is the ratio SS/df.
5.        The F-statistic, which is the ratio of the mean squares.
6.        The p-value, which is derived from the cdf of F.
The box plot of the columns of X suggests the size of the F-statistic and the p-value. Large differences in the center lines of the boxes correspond to large values of F and correspondingly small values of p.
标准方差分析表有六列:
1。变化的来源。
2。平方和(SS)由于每个源。
3所示。自由度(df)与每个源有关。
4所示。平均为每个源广场(MS),SS / df的比率。
5。的f统计量的比例意味着广场。
6。假定值,这是来自F的运作。
X的列的箱线图表明f统计量和假定值的大小。大箱子对应的中心线的差异大的F值和相应的小p值。

The anova1 function displays two figures, the standard ANOVA table and a box plot of the columns of X.
The standard ANOVA table divides the variability of the data into two parts:
•        Variability due to the differences among the column means (variability between groups)
•        Variability due to the differences between the data in each column and the column mean (variability within groups)
anova1函数显示两个数据,标准方差分析表和列的箱线图的X。
标准方差分析表中数据的变化分为两个部分:
•可变性由于差异列意味着(组)之间的差异
•可变性将在每一列数据之间的差异和列意味着(组内差异

The small p -value indicates that differences between column means are significant. The probability of this outcome under the null hypothesis (that samples drawn from the same population would have means differing by the amounts seen in X ) is equal to the p -value.


作者: 森之张卫东    时间: 2015-11-12 22:54
同学,解决没,我想学习学习!

作者: 远行的小船儿666    时间: 2016-1-11 15:39
路过的大神,如果可以给出解答,偶将不甚感激!

作者: 1345389794    时间: 2016-1-11 17:40
厉害~~~~路过····················





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5