How to calculate p-value for t-test in MATLAB?

前提是你 提交于 2020-01-06 04:12:28

问题


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))

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 = i-w }^{i+w}{(y_{s}-\widehat{y}s})^2}{(w-2)\sum{s=i-w}^{i+w}{(x_{s}-\bar{x}})^2}}$

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$.


回答1:


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)) 

and they are the same!

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);


来源:https://stackoverflow.com/questions/10617050/how-to-calculate-p-value-for-t-test-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!