In the previous mail, there are some not English comments, and may cause corruption of text.
So, I email again. The content is the same as in the previous section.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Thank you for your kind teaching. I was surprised to find such a simple solution!
In fact, I have other errors now.
If you don't mind, I would like your help to solve that error.
The area where the error is occurring is coloured red.
# doing if-then-else for not only constants
ifjudge2 <- nimbleFunction(
run = function(Nw_copy = double(), indexes = integer(2),ind_k = integer(0),N_par = integer(0)) {
for(i in 1:N_par){
if (Nw_copy[i,] != 0){
for(j in 1:Nw_copy[i,]) {
indexes[ind_k] <- i
ind_k <- ind_k+1
}
}
}
#returnType(integer(2))
return(nimList(indexes, ind_k))
})
#################### Model ########################################
particlessm <- nimbleCode({
# priors
std1 ~ dunif(0, 3000)
std2 ~ dunif(0, 1000)
for (i in 1:Npar){ # generate first particles
x[i,] ~ dunif(min = -1000, max = 2500)
y[i,] ~ dunif(min = -2500, max = 1500)
r[i,] ~ dunif(min = 1, max = 800)
a[i,] ~ dunif(min = 0, max = 60)
}
for (par_iter in 1:t) {
for(i in 1:Nsite) {
x_[,i] <- x[,1]
y_[,i] <- y[,1]
r_[,i] <- r[,1]
a_[,i] <- a[,1]
}
for (i in 1:Nsite) {
d[,i] <- pmax(sqrt((X[,i]-x_[,i])^2+(Y[,i]-y_[,i])^2)-r_[,i], 1) + 1e-10
}
for(i in 1:Npar){
Obs[i,] <- obes[par_iter,1:Nsite]
}
for (i in 1:Npar) {
diff_obe[i] <- sum(abs(a_[i,] - (Obs[i,] + k*log(d[i,])/log(10)))) # log10(x) = log(x) / log(10)
}
weight_log[1:Npar] <- -log(std2) - diff_obe/std2 # calculate log_likelihood of each particle. abs ver.
weight_max <- max(weight_log)
weight <- exp(weight_log - weight_max)
weight_normed <- weight/sum(weight)
loglikeli[par_iter] <- log(sum(weight)/Npar) + weight_max # log likelihood of time point "t"
## residual resampling ---
Nw <- Npar*weight_normed
Nw_copy[1:Npar,] <- trunc(Nw) # truncate smaller than 1
ind_k <- 1
reslist <- ifjudge2(Nw_copy = Nw_copy, indexes = indexes, ind_k = ind_k, N_par = Npar)
indexes <- reslist[1]
ind_k <- reslist[2]
}
})
#end of the model################################################################
# Const
Nparticle = 1000
Consts <- list(t = nrow(df), # the number of time point "t"
k = 10,
Npar = Nparticle,
Nsite = 5,
X = matrix(rep(c(0,941.1171,863.6608,1325.5312,1801.3538), times = Nparticle), nrow = Nparticle, byrow = TRUE),
Y = matrix(rep(c(0,583.7835,-1402.1910,-1126.3762,-843.9334), times = Nparticle), nrow = Nparticle, byrow = TRUE))
#Data
Data <- list(obs = df[,2:ncol(df)])
# Initial
Inits <- with(Consts, list(std1 = 50,
std2 = 10,
x = matrix(0,nrow = Nparticle, ncol = 1),
y = matrix(0,nrow = Nparticle, ncol = 1),
r = matrix(0,nrow = Nparticle, ncol = 1),
a = matrix(0,nrow = Nparticle, ncol = 1),
x_ = matrix(0,nrow = Nparticle, ncol = Nsite),
y_ = matrix(0,nrow = Nparticle, ncol = Nsite),
r_ = matrix(0,nrow = Nparticle, ncol = Nsite),
a_ = matrix(0,nrow = Nparticle, ncol = Nsite),
d = matrix(0,nrow = Nparticle, ncol = Nsite),
diff_obe = rep(0, Nparticle),
Obs = matrix(0,nrow = Nparticle, ncol = Nsite),
weight_log = rep(0, Nparticle),
indexes = matrix(0,nrow = Nparticle, ncol = 1),
random_uni = matrix(0,nrow = Nparticle, ncol = 1),
Nw_copy = matrix(0,nrow = Nparticle, ncol = 1)
))
particleModel <- nimbleModel(particlessm, constants = Consts,
data = Data, check = FALSE, inits = Inits)
These two errors have the same content, dimension error.
In the first red-letter place, this is the error.
Error in genVarInfo3() : Dimension of diff_obe is 1, which does not match its usage in 'weight_log[1:1000] <- -log(std2) - diff_obe/std2'.
If you comment out the above error and continue, you will get the same error in the next red section.
Error in genVarInfo3() : Dimension of Nw_copy is 2, which does not match its usage in 'reslist <- ifjudge2(Nw_copy = Nw_copy, indexes = indexes, ind_k = ind_k, N_par = 1000)'.
I think the error is probably caused by a similar reason, but I have no idea what either cause is and I am very desperate!
There are some more signs of a solution if we can find a way to know the size of the variables inside the model, but I can't find a way to do that...
Any help would be very much appreciated!
Many thanks,
kei
2023年9月20日水曜日 15:51:08 UTC+9 kei:
Dear Perry
Thank you for your kind teaching. I was surprised to find such a simple solution!
In fact, I have other errors now.
If you don't mind, I would like your help to solve that error.
The area where the error is occurring is coloured red.
# doing if-then-else for not only constants
ifjudge2 <- nimbleFunction(
run = function(Nw_copy = double(), indexes = integer(2),ind_k = integer(0),N_par = Npar) {
for(i in 1:N_par){
if (Nw_copy[i,] != 0){
for(j in 1:Nw_copy[i,]) { # num_copies[i] 個だけ i を選択する。
indexes[ind_k] <- i
ind_k <- ind_k+1
}
}
}
#returnType(integer(2))
return(nimList(indexes, ind_k))
})
#################### Model ########################################
particlessm <- nimbleCode({
# priors
std1 ~ dunif(0, 3000) # これは事前分布(MCMCで推定するパラメータの分布)
std2 ~ dunif(0, 1000)
for (i in 1:Npar){ # generate first particles
x[i,] ~ dunif(min = -1000, max = 2500) # 明示的に~行目に入れると書かないとエラー
y[i,] ~ dunif(min = -2500, max = 1500)
r[i,] ~ dunif(min = 1, max = 800)
a[i,] ~ dunif(min = 0, max = 60)
}
for (par_iter in 1:t) {
for(i in 1:Nsite) {
x_[,i] <- x[,1]
y_[,i] <- y[,1]
r_[,i] <- r[,1]
a_[,i] <- a[,1]
}
for (i in 1:Nsite) { # たぶんpmaxがverctor or scalarに対してしかできないから、ベクトルに変換してから行う
d[,i] <- pmax(sqrt((X[,i]-x_[,i])^2+(Y[,i]-y_[,i])^2)-r_[,i], 1) + 1e-10
}
for(i in 1:Npar){
Obs[i,] <- obes[par_iter,1:Nsite]
}
for (i in 1:Npar) { # ここと上は統合できそう
diff_obe[i] <- sum(abs(a_[i,] - (Obs[i,] + k*log(d[i,])/log(10)))) # 各行の合計 log10(x) = log(x) / log(10)
}
weight_log[1:Npar] <- -log(std2) - diff_obe/std2 # calculate log_likelihood of each particle. abs ver.
weight_max <- max(weight_log)
weight <- exp(weight_log - weight_max)
weight_normed <- weight/sum(weight)
loglikeli[par_iter] <- log(sum(weight)/Npar) + weight_max # log likelihood of time point "t"
## residual resampling ---
Nw <- Npar*weight_normed
Nw_copy[1:Npar,] <- trunc(Nw) # truncate smaller than 1 # この行に入れろと明示的に言わないとエラー
ind_k <- 1
reslist <- ifjudge2(Nw_copy = Nw_copy, indexes = indexes, ind_k = ind_k, N_par = Npar)
indexes <- reslist[1]
ind_k <- reslist[2]
}
})
#end of the model################################################################
# Const
Nparticle = 1000
Consts <- list(t = nrow(df), # データの行数(タイムポイント数)
k = 10,
Npar = Nparticle,
Nsite = 5, # マイク本数
X = matrix(rep(c(0,941.1171,863.6608,1325.5312,1801.3538), times = Nparticle), nrow = Nparticle, byrow = TRUE), # マイクの緯度経度
Y = matrix(rep(c(0,583.7835,-1402.1910,-1126.3762,-843.9334), times = Nparticle), nrow = Nparticle, byrow = TRUE))
#Data
Data <- list(obs = df[,2:ncol(df)])
# Initial
Inits <- with(Consts, list(std1 = 50,
std2 = 10,
x = matrix(0,nrow = Nparticle, ncol = 1),
y = matrix(0,nrow = Nparticle, ncol = 1),
r = matrix(0,nrow = Nparticle, ncol = 1),
a = matrix(0,nrow = Nparticle, ncol = 1),
x_ = matrix(0,nrow = Nparticle, ncol = Nsite),
y_ = matrix(0,nrow = Nparticle, ncol = Nsite),
r_ = matrix(0,nrow = Nparticle, ncol = Nsite),
a_ = matrix(0,nrow = Nparticle, ncol = Nsite),
d = matrix(0,nrow = Nparticle, ncol = Nsite),
diff_obe = rep(0, Nparticle),
Obs = matrix(0,nrow = Nparticle, ncol = Nsite),
weight_log = rep(0, Nparticle),
indexes = matrix(0,nrow = Nparticle, ncol = 1),
random_uni = matrix(0,nrow = Nparticle, ncol = 1),
Nw_copy = matrix(0,nrow = Nparticle, ncol = 1)
#Nw_copy = matrix(0,nrow = Npar, ncol = 1) # 正しい次元に修正
))
particleModel <- nimbleModel(particlessm, constants = Consts,
data = Data, check = FALSE, inits = Inits)
These two errors have the same content, dimension error.
In the first red-letter place, this is the error.
Error in genVarInfo3() :
Dimension of diff_obe is 1, which does not match its usage in 'weight_log[1:1000] <- -log(std2) - diff_obe/std2'.
If you comment out the above error and continue, you will get the same error in the next red section.
Error in genVarInfo3() :
Dimension of Nw_copy is 2, which does not match its usage in 'reslist <- ifjudge2(Nw_copy = Nw_copy, indexes = indexes, ind_k = ind_k, N_par = 1000)'.
I think the error is probably caused by a similar reason, but I have no idea what either cause is and I am very desperate!
There are some more signs of a solution if we can find a way to know the size of the variables inside the model, but I can't find a way to do that...
Any help would be very much appreciated!
Many thanks,
kei
2023年9月20日水曜日 11:51:16 UTC+9 Perry de Valpine: