mardi 27 janvier 2015

Double Loop for partitioning in R

How do you write a double loop partitioning a set of probability values.


First, I generated the probability values



pval <- function(n, pr, mu1, mu2, Sigma)
{ x <- rep(NA,n)
for(j in 1:n)
{ n1 <- rbinom(1,1,pr)
if (n1)
x[j] <- rnorm(1,mu1, Sigma) else
x[j] <- rnorm(1,mu2, Sigma)
p <- pnorm(x, lower.tail=F)
}
return(p)
}

n <- 100; pr <-0.25; mu1=0, mu2=1, Sigma 1
u1 <- pval(100, 0.25,0,1,1)
u1


u1 is the prob. values. Here is partitioning code



k <- 3

y=matrix(data=NA, nrow=n, ncol=k)


for(i in 1:n){
for(j in 1:k){

if (u1 > (j-1)/k & u1 < j/k)
y[i,j] <- 1 else
y[i,j] <- 0

}
}
y


but something seems to be wrong with my code 'cos I have the same partitions



[96,] 0 1 0
[97,] 0 1 0
[98,] 0 1 0
[99,] 0 1 0
[100,] 0 1 0


Can anyone spot what's wrong here?


Aucun commentaire:

Enregistrer un commentaire