我编写了以下代码,该代码应生成1行10列的输出矩阵。通过将nchrom
标签替换为q
,我在纠正循环之前确实正常工作,但现在生成以下错误代码:
Error in rmultinom(1, size * q/2, prob = c(num_chrom)) :
no positive probabilities
如果有人可以帮我确定代码中的问题所在,我将非常感激。我的代码目前如下:
randomdiv <- function(nchrom, ndivs, size) {
chrom <- matrix(nrow = 1, ncol = ndivs)
{q <- nchrom
for (i in 1:ndivs)
{
{sz <- matrix(nrow = nchrom, ncol = ndivs)
for (j in 1:nchrom) {
n <- size
for (i in 1:ndivs)
{
old_subs <- rbinom (1, n, 0.5) #roughly halving the number of subunits per chromosome, representing segregation of chromosomes
num_chrom <- rep(1 / q, q) #vector to determine probabilities for multinomial - based on number of chromosomes per cell
new_subs <- rmultinom(1, size * q / 2, prob = c(num_chrom)) #multinomial to generate randomness in number of new subunits translated per cell (based on ideal being half of the total subunit pool)
total_subs <- cbind(old_subs, new_subs) #required step to allow ifelse function to properly work on individual rows
m <- as.matrix(ifelse(total_subs[,1]>0, total_subs[,1] + total_subs[,2], total_subs[,1])) #ifelse function to ensure that if a chromosome reaches 0 subunits, there will be no new subunits added to that chromosom
zeros <- colSums(m==0) #calculates number of zeros in the columns of m and will form a vector (2 values will be shown - only interested in the first for m[1,1])
k <- c(-1, 1)
s <- sample(k, zeros[1], replace = TRUE) #random samples taken from -1 and - number of samples is equal to the number of zeros that have occurred
new_nchrom <- q + sum(s) #Sum of samples determines the number of chromosomes to add or remove from the cell (random element)
chrom[,i] <- new_nchrom #Inserts new number of chromosomes into the matrix for output
q <- new_nchrom
sz[j,i] <- m[1,1] #puts in m matrix as the next column in sz matrix - need to keep a matrix of subunit numbers because the number of subunits reaching 0 determines changes in chromosome number
n <- m
}
}
}
}
}
return (chrom)
}
>randomdiv(10, 10, 3)