[R] What' wrong?
Thomas W Blackwell
tblackw at umich.edu
Fri May 2 00:12:47 CEST 2003
Please excuse my earlier suggestions. I misunderstood
your code. (That's always a problem when trying to debug
someone else's code.)
Now I see that "prob" is defined initially as a scalar,
and the same value will be used (ncols - 1) times, so
the ifelse(...ifelse(...)) syntax is completely
unnecessary.
Failure to initialize "result" outside the loop SEEMS
not to cause a problem. Assigning to "result" is the
only place where the code uses "=" as an assignment
operator. Read the help for "=" and see whether you
think there's a scoping problem here.
In order to get the statement
print ( c(row, col, succ, trial, prob ) )
to print as a vector rather than as a list, one would
assign both probeNbr and chip as
probeNbr <- as.vector(catData[1, ])
chip <- as.vector(catData[row, ]) ,
And the error message "invalid mode ..." suggests that
the difference between a vector and a list might be
what it is complaining about. But it DOESN'T explain
to me why it got through the first iteration okay.
So to me, the mystery remains.
My own code for this would look quite different.
I truly don't understand what problem you are trying
to solve, so this may not even do what you intend,
but roughly, something like:
ncols <- dim(catData)[2]
nrows <- dim(catData)[1]
ncol1 <- ncols - 1
trial <- t(matrix(unlist(catData[1, 1:ncol1]),
ncol1, nrows - 1, byrow=FALSE))
succ <- unlist(catData[-1, 1:ncol1])
temp <- catData[-1, ncols] / catData[ 1, ncols]
temp <- ifelse(temp <= 0, 0.00000001,
ifelse(temp >= 1, 0.99999999, temp))
prob <- rep(temp, ncol1)
result <- matrix(prop.test(succ, trial, prob)$p.value,
nrows - 1, ncol1, byrow=FALSE)
dimnames(result) <- list(rownames(catData)[-1],
names(catData)[-ncols])
Now, I surely don't guarantee that this will do what you
want it to, but that's the style I would use: a single
call to prop.test() with three long vectors as the
arguments, then munge the result into the shape you
want at the end. No loops.
HTH - tom blackwell - u michigan medical school - ann arbor -
On Thu, 1 May 2003, Song, Guangchun wrote:
> I try to do single proportion test on my category data. Here is my R
> script:
>
> library("ctest")
>
> catSignifTest <- function( catFile ) {
> ###############################################################
> ## Get the data sets from text file
> catData <- read.table( catFile )
>
> ncols <- length(catData)
> nrows <- length(catData[,1])
> ncol1 <- ncols - 1
>
> probeNbr <- catData[1,]
> Achip <- catData[,ncols]
>
> for ( row in 2:nrows ) {
> prob <- Achip[ row ] / Achip[ 1 ]
> if ( prob <= 0 ) prob <- 0.0000001
> if ( prob >= 1 ) prob <- 0.9999999
> chip <- catData[row,]
> for ( col in 1:ncol1 ) {
> succ <- chip[col]
> trial <- probeNbr[col]
> print ( c(row, col, succ, trial, prob ) )
> mytest <- prop.test( succ, trial, prob )
> result [ row, col ] = mytest$p.value
> }
> }
> print ( result )
> }
>
> Here are the result:
>
> > source("D:/song/R/Test/AML/test_GO_probes.R")
> > catSignifTest( 'GOcat.txt')
> [[1]]
> [1] 2
>
> [[2]]
> [1] 1
>
> $V1
> [1] 6
>
> $V1
> [1] 30
>
> [[5]]
> [1] 0.1539431
>
> Error in min(..., na.rm = na.rm) : invalid "mode" of argument
>
> Could somebody tell me what's wrong?
>
> Thanks.
>
> Guangchun
More information about the R-help
mailing list