如何在MATLAB中为t检验计算p值? [英] How to calculate p-value for t-test in MATLAB?
问题描述
在MATLAB中是否存在一些简单的方法来计算t检验的p值.
Is there some simple way of calculating of p-value of t-Test in MATLAB.
我发现了类似的内容,但是我认为它不能返回正确的值:
I found something like it however I think that it does not return correct values:
Pval=2*(1-tcdf(abs(t),n-2))
我想计算回归斜率等于0的测试的p值.因此,我计算了标准误差
I want to calculate the p-value for the test that the slope of regression is equal to 0. Therefore I calculate the Standard Error
$ SE = \ sqrt {\ frac {\ sum_ {s = iw} ^ {i + w} {(y_ {s}-\ widehat {y} s})^ 2} {(w- 2)\ sum {s = iw} ^ {i + w} {(x_ {s}-\ bar {x}})^ 2}} $
$SE= \sqrt{\frac{\sum_{s = i-w }^{i+w}{(y_{s}-\widehat{y}s})^2}{(w-2)\sum{s=i-w}^{i+w}{(x_{s}-\bar{x}})^2}}$
其中$ y_s $是时间段$ s $中被分析参数的值,
$ \ widehat {y} _s $是时间段$ s $中被分析参数的估计值,
$ x_i $是所分析参数的观测值的时间点,
$ \ bar {x} $是从所分析的时间段然后是
$ t_ {score} =(a-a_ {0})/SE $其中$ a_ {0} $其中$ a_ {0} = 0 $.
where $y_s$ is the value of analyzed parameter in time period $s$,
$\widehat{y}_s$ is the estimated value of the analyzed parameter in time period $s$,
$x_i$ is the time point of the observed value of the analysed parameter,
$\bar{x}$ is the mean of time points from analysed period and then
$t_{score} = (a - a_{0})/SE$ where $a_{0}$ where $a_{0} = 0$.
推荐答案
我检查了ttest函数的p值和使用以下公式计算出的p值:
I checked that p values from ttest function and the one calculated using this formula:
% Let n be your sample size
% Let v be your degrees of freedom
% Then:
pvalues = 2*(1-tcdf(abs(t),n-v))
它们是相同的!
带有Matlab演示数据集的示例:
Example with Matlab demo dataset:
load accidents
x = hwydata(:,2:3);
y = hwydata(:,4);
stats = regstats(y,x,eye(size(x,2)));
fprintf('T stat using built-in function: \t %.4f\n', stats.tstat.t);
fprintf('P value using built-in function: \t %.4f\n', stats.tstat.pval);
fprintf('\n\n');
n = size(x,1);
v = size(x,2);
b = x\y;
se = diag(sqrt(sumsqr(y-x*b)/(n-v)*inv(x'*x)));
t = b./se;
p = 2*(1-tcdf(abs(t),n-v));
fprintf('T stat using own calculation: \t\t %.4f\n', t);
fprintf('P value using own calculation: \t\t %.4f\n', p);
这篇关于如何在MATLAB中为t检验计算p值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!