Analysis using the Friedman Test

The data are within-subjects and ordinal (possible values are integers -2 to 2) at every vertex (it’s surface data in the group space).
Here is the R code:

#---- R script for carrying out friedman test
#---- this is a test script to use functions in the aggregate
FriedmanStat <- function(x){
    mm <- matrix(nrow=24,ncol=3)
    mm[,1] = x[1:24]
    mm[,2] = x[25:48]
    mm[,3] = x[49:72]
    return(friedman.test(mm)[[1]][[1]])
}

FriedmanP <- function(x){
    mm <- matrix(nrow=24,ncol=3)
    mm[,1] = x[1:24]
    mm[,2] = x[25:48]
    mm[,3] = x[49:72]
    return(friedman.test(mm)[[3]][[1]])
}

Query_out <- as.matrix(read.table("test_aov.lh.txt"))
data_stack <- stack(data.frame(Query_out[,3:5]))[1]
vertices <- Query_out[,2]
nn <- data.frame(cbind(vertices,data_stack))
m <- matrix(nrow=length(as.integer(levels(as.factor(vertices)))),ncol=3)
m[,1] <- as.integer(levels(as.factor(vertices)))
m[,2] <- aggregate(nn$values, list(nn$vertices),FriedmanStat)[,2]
m[,3] <- 1 - (aggregate(nn$values, list(nn$vertices),FriedmanP)[,2])
write.table(round(m,5), file=paste("test.txt",sep=""), row.names=FALSE, col.names=FALSE, quote=F)

The query that will be used:

select subject, vertex, speech_lag, emblem_lag, embspeech_lag from ccf_phase2_rh where seed_region = 'region' and vertex between BEGIN_BATCH and END_BATCH;

where BEGIN_BATCH and END_BATCH are unique batch sets established in the Swift code.
The Swift code uses our “Mediator” app that instantiates database queries on the grid sites:

type file{}

(file qout, file rout) run_query (string allcatargs, file config, file r_script){
    app{
        Mediator allcatargs stdout=@filename(qout) @filename(r_script);
    }
}

string user = @arg("user");
string db = "yourdb";
string host = "yourhost";
string baseid = "rh_NonParam";

file r_script<single_file_mapper; file="scripts/FriedmanTest.R">;
file config<single_file_mapper; file="user.config">;

loop_query(int bvox, string user, string db, string host, string query_outline, file r_script, file config, string id){
    int evox = bvox+499;
    string r_swift_args = @strcat(id);
    string theoutprefix = @strcat(id,bvox,"_",evox);
    string med_args = @strcat("--user ","andric",
        " --conf ", "user.config",
        " --db ", db,
        " --host ", host,
        " --query ", query_outline,
        " --r_script ", @filename(r_script),
        " --begin_vox ", bvox,
        " --end_vox ", evox,
        " --outprefix ", theoutprefix,
        " --batchstep ", "500",
        " --r_swift_args ", r_swift_args,
        " --subject ", id);
    file q_result <single_file_mapper; file=@strcat("results/",theoutprefix,".qresult")>;
    file r_result <single_file_mapper; file=@strcat(theoutprefix,".txt")>;
    (q_result, r_result) = run_query(med_args, r_script, config);
}

string regions = ["IDEAL"];
foreach region in regions {
    int mybatches = [1:196000:500];
    foreach batch in mybatches {
        string id = @strcat(region, baseid);
        string query_outline = @strcat("select subject, vertex, speech_lag, emblem_lag, embspeech_lag from ccf_phase2_rh where seed_region = '",region,"' and vertex between BEGIN_BATCH and END_BATCH");
        loop_query(batch, user, db, host, query_outline, r_script, config, id);
    }
}

It would also be useful to embed the hemispheres in a for loop to distribute via Swift. As this is, it’s my “rh” version – and I also have a “lh” version that queries the left hemisphere data.

One Response to “Analysis using the Friedman Test”

  1. Permutation distribution of Friedman Test data « Neuroimaging with SQL Says:

    [...] the configuration file, it performs the same db query and is otherwise much the same code in the previous post for executing the Friedman [...]


Leave a Reply