How to resolve the algorithm Permutation test step by step in the Julia programming language
How to resolve the algorithm Permutation test step by step in the Julia programming language
Table of Contents
Problem Statement
A new medical treatment was tested on a population of
n + m
{\displaystyle n+m}
volunteers, with each volunteer randomly assigned either to a group of
n
{\displaystyle n}
treatment subjects, or to a group of
m
{\displaystyle m}
control subjects.
Members of the treatment group were given the treatment,
and members of the control group were given a placebo.
The effect of the treatment or placebo on each volunteer
was measured and reported in this table.
Write a program that performs a
permutation test to judge
whether the treatment had a significantly stronger effect than the
placebo.
Extremely dissimilar values are evidence of an effect not entirely due to chance, but your program need not draw any conclusions. You may assume the experimental data are known at compile time if that's easier than loading them at run time. Test your solution on the data given above.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Permutation test step by step in the Julia programming language
This Julia code implements a permutation test to compare the means of two independent groups of data.
The code first defines a function, meandiff
, to compute the difference between the means of two vectors. It then defines a function, bifurcate
, to split a vector into two new vectors based on a selection vector.
The permutation_test
function takes two vectors, treated
and control
, as input and generates all possible permutations of the data in treated
and control
. For each permutation, it calculates the difference between the means of the two groups and compares it to the observed difference between the means of the original treated
and control
groups. It counts the number of permutations that result in a difference greater than or equal to the observed difference (better) and the number of permutations that result in a difference less than the observed difference (worse).
In the main part of the code, two vectors of data are defined, treated
and control
. The permutation_test
function is then called with these two vectors, and the results are stored in the variables better
and worse
. The total number of permutations is also calculated and stored in the variable tot
. The results are then printed to the console, showing the total number of permutations, the percentage of permutations that showed better results than the observed difference, and the percentage of permutations that showed worse results or equal results.
Overall, this code provides a simple and efficient way to perform a permutation test to compare the means of two independent groups of data.
Source code in the julia programming language
using Combinatorics
meandiff(a::Vector{T}, b::Vector{T}) where T <: Real = mean(a) - mean(b)
function bifurcate(a::AbstractVector, sel::Vector{T}) where T <: Integer
x = a[sel]
asel = trues(length(a))
asel[sel] = false
y = a[asel]
return x, y
end
function permutation_test(treated::Vector{T}, control::Vector{T}) where T <: Real
effect0 = meandiff(treated, control)
pool = vcat(treated, control)
tlen = length(treated)
plen = length(pool)
better = worse = 0
for subset in combinations(1:plen, tlen)
t, c = bifurcate(pool, subset)
if effect0 < meandiff(t, c)
better += 1
else
worse += 1
end
end
return better, worse
end
const treated = [85, 88, 75, 66, 25, 29, 83, 39, 97]
const control = [68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
(better, worse) = permutation_test(treated, control)
tot = better + worse
println("Permutation test using the following data:")
println("Treated: ", treated)
println("Control: ", control)
println("\nThere are $tot different permuted groups of these data.")
@printf("%8d, %5.2f%% showed better than actual results.\n", better, 100 * better / tot)
print(@sprintf("%8d, %5.2f%% showed equalivalent or worse results.", worse, 100 * worse / tot))
You may also check:How to resolve the algorithm Additive primes step by step in the TSE SAL programming language
You may also check:How to resolve the algorithm Dot product step by step in the Perl programming language
You may also check:How to resolve the algorithm Scope modifiers step by step in the Axe programming language
You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Parameterized SQL statement step by step in the F# programming language