Creating a function that preforms two sample t-tests with unequal variances
12 views (last 30 days)
Show older comments
Hello -- I am pretty new at Matlab, I am currently towards the end of a one-semeter course. For this reason please don't use fancy technical jargon in your answers becuase I'm going to have no idea what you're saying.
My main problem is, is that I don't how how to assign vartype. I have on a second page all of the defined values for the things like MX and pooled variance. I have googled for literally hours on how to assign vartype and it seems that everyone has a different process, and none of them are working for my function. It seems a lot of people like strings:
function [tStat, df, pvalue] = newttest(X,Y,vartype)
%this function performs pooled and unpooled t-tests
%this function uses unpaired two sample t-tests
if vartype==1
%Performing a pooled variance t-test
%if the distributions are equal then compute pooled variance t-test
tStat = (MX/MY)/(sqrt(pooledvariance)/(1/n1+1/n2))
df = n1+n2-2
pValue = 2 * tcdf(-abs(tStat), df);
else vartype==2
%Performing an unpooled variance t-test
%if distributions are unequal then compute unpooled variance t-test
tStat = (MX/MY)/sqrt((Xvar/n1)+(Yvar/n2))
df = ((Xvar/n1+Yvar/n2)^2)/((Xvar/n1)^2)*(1/(n1-1))+((Yvar/n2)^2)*(1/(n2-1))
pValue = 2 * tcdf(-abs(tStat), df);
end
^here is my function. I built the function from a skeleton that my professor provided.
When I run this code, I recieve:
>> newttest
Not enough input arguments.
Error in newttest (line 5)
if vartype==1
I'm sure there's a simple solution to this but I have no idea what it is.
0 Comments
Answers (1)
Star Strider
on 13 Apr 2020
‘My main problem is, is that I don't how how to assign vartype.’
I’m not certain what you’re asking. It appears that ‘vartype’ is either 1 or 2, depending on the test you want to do.
.
3 Comments
Star Strider
on 13 Apr 2020
‘Am I doing someting else wrong?’
Yes, sort of.
Several variables are not defined: ‘pooledvariance’, ‘Xvar’ and ‘Yvar’. The funciton will not automatically assign the data inputs to ‘Xvar’ and ‘Yvar’. They need to be assigned specifically. The ‘pooledvariance’ variable is completely missing, at least in the code you posted.
‘I'm not sure what is mean to pass X and Y as inputs?’
The inputs are the function arguments: ‘X’, ‘Y’, and ‘vartype’. The first two need to be vectors, and the last one is a scalar.
.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!