warning in a loop
You can investigate what's gone wrong after the loop has failed by looking
at the values of i, k, p, and t. Although d[(d[,(i+1)]%in%1),1] produces a
vector, k has only one element. Same with p. Should then be obvious why the
t.test produces an error.
The problem is with the [i] index for k and p; take those away and it works.
If you want to keep the values generated in the loop, make k and p lists and
index with k[[i]] and p[[i]].
A better way to do this would be to use 'sample' to randomize the measured
values in d[,1] and then use d[,2] to group them for testing. You can then
use hundreds of iterations:
t <- rep(NA, 999)
for(i in 1:999) {
samp <- sample(d[,1])
t <- t.test(samp[d[,2]==1], samp[d[,2]==2])$p.value
}
sum(t < 0.05) # How many were 'significant'?
Note that I prefer to use t <- rep(NA,...) to allocate space, rather than
1:999, so that NA appears as the result if there's a problem.
Why not just do a randomization test?
t <- rep(NA, 1000)
t[1] <- mean(d[d[,2]==1,1]) - mean(d[d[,2]==2,1]) # This is the observed
difference in means
for(i in 2:1000) {
samp <- sample(d[,1])
t[i] <- mean(samp[d[,2]==1]) - mean(samp[d[,2]==2])
}
t <- abs(t) # Skip this line if you want a 1-sided test
sum(t >= t[1])/1000 # This is the 'p-value'
HTH, Mike.
Tavpritesh Sethi wrote:
hi all,
I have a matrix with first column having some measurable values, these are
indexed by the numerals 1,2 and 3 in the other columns of the data and may
be interpreted as values for categories 1,2 and 3.
I have written the following loop
t<-1:10
for(i in 1:10)
+ {
+ k[i]<-d[(d[,(i+1)]%in%1),1]
+ p[i]<-d[(d[,(i+1)]%in%2),1]
+ t[i]<-t.test(k[i],p[i])$p.value
+ }
Error in t.test.default(k[i], p[i]) : not enough 'x' observations
In addition: Warning messages:
1: number of items to replace is not a multiple of replacement length
2: number of items to replace is not a multiple of replacement length
As you might have understood, I want to test for difference between the
two
cagories: "k" and "v". the second column of the data is the original
categorization and the rest columns(3:10) are a matrix of randomized
values
between 1 to 3. (I have three categories)
My purpose of doing so is to check whether significant difference comes up
in the randomized data also. This is to check the effect of the small
sample
size of my data.
Please suggest a way or an alternative to the above approach.
[[alternative HTML version deleted]]
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
View this message in context: http://www.nabble.com/warning-in-a-loop-tf3969850.html#a11273379 Sent from the R help mailing list archive at Nabble.com.