2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MATLABのboxplotでノッチで中央値の有為差比較する範囲を1%水準にする

Last updated at Posted at 2014-03-22

デフォルトのboxplotのノッチは,

MathWorks 箱ひげ図 より
区間がオーバーラップしない場合、2 つの中央値は 5% の水準で有意差があります。区間の端点は、刻みまたは三角形のマーカーの中心の極値です。極値は q2 – 1.57(q3 – q1)/sqrt(n) と q2 + 1.57(q3 – q1)/sqrt(n) に対応します。ここで、q2 は中央値 (50 番目の百分位数)、q1 と q3 はそれぞれ 25 番目および 75 番目の百分位数であり、n は NaN 値を含めない観測値の数です。標本のサイズが小さい場合、刻みはボックスの端を超える可能性があります。

とのこと.

それぞれの中央値を1%の水準で有為差比較するノッチを作るため,(意味違うけど)q4とq5という変数に,それぞれ5番目および95番目の百分位数を保存しておき,ノッチの上端と下端を q2 – 1.57(q5 – q4)/sqrt(n) と q2 + 1.57(q5 – q4)/sqrt(n) とした.

diff置いとく.

boxplot.m.diff
--- /Applications/MATLAB_R2012a_Student.app/toolbox/stats/stats/boxplot.m.orig	2014-03-22 12:24:26.000000000 +0900
+++ /Applications/MATLAB_R2012a_Student.app/toolbox/stats/stats/boxplot.m	2014-03-24 15:32:15.000000000 +0900
@@ -2097,6 +2097,8 @@
 boxidx.q1 = nan;
 boxidx.q2 = nan;
 boxidx.q3 = nan;
+boxidx.q4 = nan;
+boxidx.q5 = nan;
 boxidx.whi = nan;
 boxidx.maximum = nan;
 boxidx.numNans = 0;
@@ -2127,9 +2129,11 @@
 ind0 = gStart-1;
 % Calculate 25,50,75 percentile indices.
 if lastind > 1
+    i05 = firstind+ .05*(lastind-firstind) -.45;   % Edit to set 1% for confidence interval
     i25 = firstind+ .25*(lastind-firstind) -.25;
     i50 = firstind+ .50*(lastind-firstind);
     i75 = firstind+ .75*(lastind-firstind) +.25;
+    i95 = firstind+ .95*(lastind-firstind) +.45;   % Edit to set 1% for confidence interval
 else
     i25 = 1;
     i50 = 1;
@@ -2195,6 +2199,8 @@
 boxidx.q1 = i25+ind0;
 boxidx.q2 = i50+ind0;
 boxidx.q3 = i75+ind0;
+boxidx.q4 = i05+ind0;   % Edit to set 1% for confidence interval
+boxidx.q5 = i95+ind0;   % Edit to set 1% for confidence interval
 boxidx.whi = whi+ind0;
 boxidx.maximum = lastind+ind0;
 boxidx.outliers = {outall+ind0};
@@ -2235,6 +2241,8 @@
 i25 = boxidx.q1-ind0;
 i50 = boxidx.q2-ind0;
 i75 = boxidx.q3-ind0;
+i05 = boxidx.q4-ind0;   % Edit to set 1% for confidence interval
+i95 = boxidx.q5-ind0;   % Edit to set 1% for confidence interval
 
 % Compute weighted average.
 p25Ratio = i25-floor(i25);
@@ -2261,6 +2269,24 @@
         +x(ceil(i75))*p75Ratio;
 end
 
+% Edit to set 1% for confidence interval
+p05Ratio = i05-floor(i05);
+if p05Ratio==0
+    p05 = x(i05);
+else
+    p05 = x(floor(i05))*(1-p05Ratio) ...
+        +x(ceil(i05))*p05Ratio;
+end
+
+% Edit to set 1% for confidence interval
+p95Ratio = i95-floor(i95);
+if p95Ratio==0
+    p95 = x(i95);
+else
+    p95 = x(floor(i95))*(1-p95Ratio) ...
+        +x(ceil(i95))*p95Ratio;
+end
+
 % Standard percentile function is slower.
 %     p = prctile(x,[25 50 75]);
 %     p25 = p(1);
@@ -2271,11 +2297,13 @@
 % x may be padded with NaN -- don't count them
 nx = sum(~isnan(x));
 
-nhi = p50 + 1.57*(p75-p25)/sqrt(nx);
+%nhi = p50 + 1.57*(p75-p25)/sqrt(nx);
+nhi = p50 + 1.57*(p95-p05)/sqrt(nx);   % Edit to set 1% for confidence interval
 if ~isfinite(nhi)
     nhi = p50;
 end
-nlo = p50 - 1.57*(p75-p25)/sqrt(nx);
+%nlo = p50 - 1.57*(p75-p25)/sqrt(nx);
+nlo = p50 - 1.57*(p95-p05)/sqrt(nx);   % Edit to set 1% for confidence interval
 if ~isfinite(nlo)
     nlo = p50;
 end

手抜きして,既存の関数を直接編集しちゃった.
MATLABを再起動したら読み込まれる.

後で混乱するので,作図したら元に戻しておくこと.

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?