Welch's t-test: Difference between revisions

J draft
(Corrected error in equations, added to task description and corrected typo)
(J draft)
Line 157:
Test sets 4 p-value = 0.090773
Test sets 5 p-value = 0.010751</pre>
 
=={{header|J}}==
 
Implementation:
 
<lang J>integrate=: adverb define
'a b steps'=. 3{.y,128
size=. (b - a)%steps
size * +/ u |: 2 ]\ a + size * i.>:steps
)
simpson =: adverb def '6 %~ +/ 1 1 4 * u y, -:+/y'
 
lngamma=: ^.@!@<:`(^.@!@(1 | ]) + +/@:^.@(1 + 1&| + i.@<.)@<:)@.(1&<:)"0
mean=: +/ %#
nu=: # - 1:
sampvar=: +/@((- mean) ^ 2:) % nu
ssem2=: sampvar % #
welch_T=: -&mean % 2 %: +&ssem2
nu=: nu f. : ((+&ssem2 ^ 2:) % +&((ssem2^2:)%nu))
B=: ^@(+&lngamma - lngamma@+)
 
p2_tail=:dyad define
t=. x welch_T y NB. need numbers for numerical integration
v=. x nu y
F=. ^&(_1+v%2) % 2 %: 1&-
lo=. 0
hi=. v%(t^2)+v
(F f. simpson integrate lo,hi) % 0.5 B v%2
)</lang>
 
integrate and simpson are from the [[Numerical_integration#J|Numerical integration]] task.
 
lngamma is from http://www.jsoftware.com/pipermail/programming/2015-July/042174.html
 
mean is classic J - most J tutorials will include this
 
The initial definition of nu (degrees of freedom of a data set), as well as the combining form (approximating degrees of freedom for two sets of data) is from [[wp:Welch%27s_t_test#Calculations|Welch's t test]]. (Verb definitions can be forward referenced, even in J's tacit definitions, but it seems clearer to specify these definitions so they only depend on previously declared definitions.)
 
sampvar is sample variance (or: standard deviation squared)
 
ssem2 is squared standard error of the mean
 
Data for task examples:
<lang J>d1=: 27.5 21 19 23.6 17 17.9 16.9 20.1 21.9 22.6 23.1 19.6 19 21.7 21.4
d2=: 27.1 22 20.8 23.4 23.4 23.5 25.8 22 24.8 20.2 21.9 22.1 22.9 20.5 24.4
d3=: 17.2 20.9 22.6 18.1 21.7 21.4 23.5 24.2 14.7 21.8
d4=: 21.5 22.8 21 23 21.6 23.6 22.5 20.7 23.4 21.8 20.7 21.7 21.5 22.5 23.6 21.5 22.5 23.5 21.5 21.8
d5=: 19.8 20.4 19.6 17.8 18.5 18.9 18.3 18.9 19.5 22
d6=: 28.2 26.6 20.1 23.3 25.2 22.1 17.7 27.6 20.6 13.7 23.2 17.5 20.6 18 23.9 21.6 24.3 20.4 24 13.2
d7=: 30.02 29.99 30.11 29.97 30.01 29.99
d8=: 29.89 29.93 29.72 29.98 30.02 29.98
d9=: 3 4 1 2.1
da=: 490.2 340 433.9</lang>
 
Task examples:
<lang J> d1 p2_tail d2
0.021378
d3 p2_tail d4
0.148842
d5 p2_tail d6
0.0359723
d7 p2_tail d8
0.0907733
d9 p2_tail da
0.0107377</lang>
 
=={{header|R}}==
6,962

edits