How to repeat a process N times but considering the previous result before doing the next one?

42 Views Asked by At

I need to repeat all this 100 times in order to have different values for heterozygousHWE, but I have to consider the previous result before doing the next. I mean to do the loop 2 I need to take the result that I have on the first loop, because heterozygousHWE must decrease in each loop.

n11 <- 10
n12 <- 12
n22 <- 3
#total de individuos
N <- n11 + n12 + n22

#calculos de frecuecnias alelicas

n1 <- (2*n11) + n12

n2 <- (2*n22) + n12

p1 = n1 /(2*N)
p1

p2 = 1- p1
p2
#TODO: Calcular p-value prueba chi cuadrada

#ESPERADOS 

n11e= N * (p1^2)

n12e = 2*N *p1*p2

n22e= N*((p2)^2)


#observados - esperados 

restn11 = n11- n11e
restn11

restn12 = n12- n12e
restn12

restn22 = n22- n22e
restn22

# (O-E)^2/2

x11= (restn11^2)/n11e
x11
x12= (restn12^2)/n12e
x12
x22= (restn22^2)/n22e
x22

#suma total

TOTAL = x11 + x12 + x22

#x^2
TOTAL

#valor P (p-value)

vp1gl= 0.4549
vp2gl= 1.3863

pchisq(TOTAL ,df=1)

#Simular poblacion HWE

#numbers GENERA 200 n aleatorios
#A1 cuantas personas son mayores a p1


numbers <- runif(N)
numbers

a1 <- numbers>p1
a1

numbers <- runif(N)
a2 <- numbers>p1
a2

genotypes <- a1 + a2
heterozygousHWE <- sum (genotypes==1)
heterozygousHWE
    


get.bias <- function(i) {
  numbers <- runif(N)
  numbers
  
  a1 <- numbers>p1
  a1
  
  numbers <- runif(N)
  a2 <- numbers>p1
  a2
  
  genotypes <- a1 + a2
  heterozygousHWE <- sum (genotypes==1)
  heterozygousHWE
}
set.seed(1)
result <- t(sapply(1:1000,get.bias))
m <- head(result)
m

I don't know how to implement the loop considering the previous result.

I know how to repeat the process the 100 times but it seems like each time it is a new loop that does not consider the previous result.

0

There are 0 best solutions below