Last active
August 29, 2015 14:21
-
-
Save JohnArchieMckown/4da7138670aae17c917f to your computer and use it in GitHub Desktop.
R code for "vincentize", originally written by Paul Lemmens, gotten from https://stat.ethz.ch/pipermail/r-help/2003-May/034272.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| vincentize <- function(data, bins) | |
| { | |
| if ( length(data) < 2 ) | |
| { | |
| stop("The data is really short. Is that ok?"); | |
| } | |
| if ( bins < 2 ) | |
| { | |
| stop("A number of bins smaller than 2 just really isn't useful"); | |
| } | |
| if ( bins > length(data) ) | |
| { | |
| stop("This is really unusual, although perhaps possible. If you really know what you're doing, maybe you should disable this check!?."); | |
| } | |
| ret <- c(); | |
| for ( i in 1:length(data)) | |
| { | |
| rt <- data[i]; | |
| b <- 0; | |
| while ( b < bins ) | |
| { | |
| ret <- c(ret, rt); | |
| b <- b+1; | |
| } | |
| } | |
| ret; | |
| } | |
| binify <- function(data, bins, n) | |
| { | |
| if ( bins < 2 ) | |
| { | |
| stop("Number of bins is smaller than 2. Nothing to split, exiting."); | |
| } | |
| if ( length(data) < 2 ) | |
| { | |
| stop("The length of the data is really short. Is that ok?"); | |
| } | |
| if ( bins * n != length(data) ) | |
| { | |
| stop("Cannot construct bins of equal length."); | |
| } | |
| t(array(data, c(n,bins))); | |
| } | |
| mean.bins <- function(data) | |
| { | |
| # For the vincentizing procedures in vincentize() and binify(), | |
| # it made sense to check the data array/vector/matrix. Here, | |
| # we now just need to check that data is a matrix. | |
| if ( !is.matrix(data) ) | |
| { | |
| stop("The data is not in matrix form."); | |
| } | |
| means <- c(); | |
| bins <- dim(data)[1]; | |
| for (i in 1:bins) | |
| { | |
| means <- c(means, mean(data[i,])); | |
| } | |
| # return a vector of means. | |
| means; | |
| } | |
| bins.factor <- function(data, bins) | |
| { | |
| if ( !is.data.frame(data) ) | |
| { | |
| stop("data is not a data frame."); | |
| } | |
| source('Ratcliff.r', local=TRUE); | |
| subject.bin.means <- c(); | |
| attach(data); | |
| l <- levels(Cond); | |
| for ( i in 1:length(l) ) | |
| { | |
| cat("Calculating bins for factor level ", l[i], ".\n", sep=""); | |
| flush.console(); | |
| data <- RT[Cond == l[i]]; | |
| data <- sort(data); | |
| n <- length(data); | |
| data.vincent <- vincentize(data,bins); | |
| data.vincent.bins <- binify(data.vincent, bins, n); | |
| bin.means <- mean.bins(data.vincent.bins); | |
| # FAILING TEST. | |
| mean.orig <- mean(data); | |
| mean.b <- mean(bin.means); | |
| # if ( mean.b != mean.orig ) | |
| if (identical(all.equal(mean.b, mean.org, tolerance=.Machine$double.eps),FALSE) | |
| { | |
| #cat("mean.b\n", str(mean.b), "mean.orig\n", str(mean.orig)); | |
| flush.console; | |
| detach(data); | |
| stop("Something went wrong calculating the bins: means do not equal."); | |
| } | |
| subject.bin.means <- c(subject.bin.means, bin.means); | |
| } | |
| detach(data); | |
| if ( !length(subject.bin.means) == bins*length(l) ) | |
| { | |
| stop("Inappropriate number of means calculated."); | |
| } | |
| else | |
| { | |
| subject.bin.means | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment