Cannot evaluate subset expression in Jags - jags

I am sort of new to Jags, and I have been racking my brain trying to see what I am doing wrong with my code, which is giving this error message:
'Cannot evaluate subset expression for precd'.
I am attaching my code which has the data coded in it, as well. I'd appreciate all the help I can get.
nS=number of studies,
r=number of events,
n=sample size,
tau.sq=heterogeneity,
na=number of arms,
sw='correction' for multi-arm trials.
Model<- function(){
for(i in 1:nS) {
w[i,1]<- 0
theta[i,t[i,1]]<- 0
for (k in 1:na[i]) {r[i,t[i,k]] ~ dbin(p[i,t[i,k]],n[i,t[i,k]])}
logit(p[i,t[i,1]])<- u[i]
for (k in 2:na[i]) {
logit(p[i,t[i,k]])<- u[i] + theta[i,t[i,k]]
theta[i,t[i,k]] ~ dnorm(md[i,t[i,k]],precd[i,t[i,k]])
md[i,t[i,k]]<- mean[i,k] + sw[i,k]
w[i,k]<- (theta[i,t[i,k]] - mean[i,k])
sw[i,k]<- sum(w[i,1:k-1])/(k-1)
precd[i,t[i,k]]<- prec*2*(k-1)/k
mean[i,k] <-d[t[i,k]] - d[t[i,1]]
}}
Priors for basic parameters
for (i in 1:nS) {u[i] ~ dnorm(0,.01)}
tau ~ dnorm(0,.01)
prec<- 1/pow(tau,2)
tau.sq<- pow(tau,2)
d[ref] <- 0
for(k in 1:(ref-1)) {d[k] ~ dnorm(0,.01)}
for(k in (ref+1):nT) {d[k] ~ dnorm(0,.01)}
Collection of results from this point
for(i in 1:(nT-1)) {
for (j in (i+1):nT) {
OR[j,i]<- exp(d[j] - d[i])
LOR[j,i]<- d[j] - d[i]}}
for(j in 1:(ref-1)){ORref[j]<- exp(d[j] - d[ref])}
for(j in (ref+1):nT) {ORref[j]<- exp(d[j] - d[ref])}
Ranking of treatments
for(k in 1:nT) {
order[k]<- rank(d[],k)
most.effective[k]<-equals(order[k],1)
for(j in 1:nT) {effectiveness[k,j]<- equals(order[k],j)
cumeffectiveness[k,j]<- sum(effectiveness[k,1:j])}}
for(k in 1:nT) {
SUCRA[k]<- sum(cumeffectiveness[k,1:(nT-1)]) /(nT-1)
}
for(i in 1:nS) {
for (k in 1:na[i]) {
Darm[i,k]<- -2*( r[i,t[i,k]] *log(n[i,t[i,k]]*p[i,t[i,k]]/
r[i,t[i,k]])+(n[i,t[i,k]] - r[i,t[i,k]])*log((n[i,t[i,k]]-
n[i,t[i,k]]* p[i,t[i,k]])/(n[i,t[i,k]]- r[i,t[i,k]])))}
D[i]<- sum(Darm[i,1:na[i]])}
D.bar<- sum(D[])
}
The data are coded below
library(R2jags)
library(mcmc)
setwd("PATH-NAME")
# *******************************************************
# Data are coded below:
# BA -1
# BMS -2 *** REFERENCE
# DCB -3
# EES -4
# PES -5
# ROTA -6
# SES -7
# VBT -8
# *******************************************************
t_RCT = structure(.Data=c(1,6,NA,
1,6,NA,
1,2,NA,
1,2,NA,
2,6,NA,
1,2,NA,
1,8,NA,
1,8,NA,
1,8,NA,
5,8,NA,
5,8,NA,
1,5,7,
1,7,NA,
7,8,NA,
7,8,NA,
5,7,NA,
1,7,NA,
1,7,NA,
4,7,NA,
3,4,NA,
3,4,NA,
1,3,NA,
3,5,NA,
1,3,NA,
1,3,NA,
1,3,NA,
1,3,5,
3,5,NA), .Dim=c(28,3))
na_RCT <- c(2,2,2,2,2,2,2,2,2,2,2,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,2)
r_RCT = structure(.Data=c(45,NA,NA,NA,NA,60,NA,NA,
45,NA,NA,NA,NA,32,NA,NA,
65,55,NA,NA,NA,NA,NA,NA,
5,1,NA,NA,NA,NA,NA,NA,
NA,8,NA,NA,NA,12,NA,NA,
7,4,NA,NA,NA,NA,NA,NA,
51,NA,NA,NA,NA,NA,NA,32,
76,NA,NA,NA,NA,NA,NA,57,
21,NA,NA,NA,NA,NA,NA,11,
NA,NA,NA,NA,19,NA,NA,40,
NA,NA,NA,NA,0,NA,NA,0,
33,NA,NA,NA,19,NA,8,NA,
24,NA,NA,NA,NA,NA,12,NA,
NA,NA,NA,NA,NA,NA,64,39,
NA,NA,NA,NA,NA,NA,3,12,
NA,NA,NA,NA,30,NA,35,NA,
8,NA,NA,NA,NA,NA,8,NA,
3,NA,NA,NA,NA,NA,0,NA,
NA,NA,NA,2,NA,NA,1,NA,
NA,NA,6,1,NA,NA,NA,NA,
NA,NA,20,7,NA,NA,NA,NA,
21,NA,5,NA,NA,NA,NA,NA,
NA,NA,4,NA,10,NA,NA,NA,
10,NA,1,NA,NA,NA,NA,NA,
14,NA,11,NA,NA,NA,NA,NA,
22,NA,4,NA,NA,NA,NA,NA,
56,NA,30,NA,17,NA,NA,NA,
NA,NA,16,NA,11,NA,NA,NA), .Dim=c(28,8))
n_RCT = structure(.Data=c(146,1,1,1,1,152,1,1,
100,1,1,1,1,100,1,1,
226,224,1,1,1,1,1,1,
29,29,1,1,1,1,1,1,
1,25,1,1,1,30,1,1,
20,20,1,1,1,1,1,1,
121,1,1,1,1,1,1,131,
232,1,1,1,1,1,1,244,
78,1,1,1,1,1,1,78,
1,1,1,1,195,1,1,201,
1,1,1,1,20,1,1,17,
100,1,1,1,100,1,100,1,
74,1,1,1,1,1,76,1,
1,1,1,1,1,1,259,125,
1,1,1,1,1,1,65,64,
1,1,1,1,225,1,225,1,
61,1,1,1,1,1,136,1,
48,1,1,1,1,1,48,1,
1,1,1,34,1,1,32,1,
1,1,95,94,1,1,1,1,
1,1,154,155,1,1,1,1,
54,1,54,1,1,1,1,1,
1,1,66,1,65,1,1,1,
25,1,25,1,1,1,1,1,
38,1,72,1,1,1,1,1,
72,1,138,1,1,1,1,1,
134,1,137,1,131,1,1,1,
1,1,109,1,106,1,1,1), .Dim=c(28,8))
dataR <- list(nS=28, nT=8, ref=2, t=t_RCT, na=na_RCT, r=r_RCT, n=n_RCT)
initR<-list(list(u=rep(0,28), tau=0.5),list(u=rep(0.2,28), tau=0.7))
Params<-c("u","OR")
ModelFit<-jags(data=dataR, inits=initR, Params, n.chains=2,
n.iter=5000, n.burnin=2500, model.file=Model, n.thin=5)
Result<-ModelFit$BUGSoutput$summary
write.table(round(Result,digits=3),
"Results.txt",row.names=T,col.names=T)
DIC<- ModelFit$BUGSoutput$DIC
write.table(round(DIC,digits=3), "Dic.txt",row.names=T,col.names=T)
postscript("PostDen.ps")
ModelFit.mcmc <- as.mcmc(ModelFit)
densplot(ModelFit.mcmc)
dev.off()

You are subsetting your data with the object t, which within your R environment, is t_RCT. This object has NA values within it. So, for example, if i = 1 and k = 3 then you are trying to subset the object p in JAGS as:
p[i,t[i,k]] = p[1,NA]
You cannot subset like this, as rows and columns must be indexed as an integer.

Related

Can Jarvis marching algorith return UNso0rted results?

I am trying to compute a complex hull for a very complex point set. See picture:
The green/teal line to the left contains convex hull points that are neighboring, i.e. come directly after one another. continuing the sequence, the next point should be the cyan/green one in right bottom.
However, if i walk along the array, the next one appears to be the one in top right, in yellow.
Yes, output is in ascii, point cloud is correct (compared with GUI output) .
Snippet:
for(int i = 0; i< rd.length; i++) {
if( (*rd)[i].lon < lonMin) {
lonMin = (*rd)[i].lon;
leftMostPos = i;
}
}
int [] hullIdx = new int[0];
int p = leftMostPos, m = to!int((*rd).length), q;
do {
hullIdx ~= [p];
q = (p + 1) % m;
for ( int ij = 0; ij < m ; ij ++) {
auto a = (*rd)[p];
auto b = (*rd)[ij];
auto c = (*rd)[q];
double ornt = (b.lat - a.lat) * ( c.lon - b.lon) - (b.lon - a.lon) * (c.lat - b.lat) ;
if (ornt < 0) q = ij;
}
p = q;
} while ( p != leftMostPos);
Language is D, I am solving for Geographical Lats and Lons. Lons are taken as a coordinates, lats are in x coordinate. The Matrix increases y coordinate by row, towards downward direction. This is also taken into account.
I would like to know how come Jarvis algorithm returns UNsorted output. I would apppreciate all your help with gratefulness.

RCPP and the %*% operator, revisited

I'm trying to decide if it makes sense to implement R's %*% operator in RCpp
if my dataset is huge. BUT, I am really having trouble getting a RCpp implementation.
Here is my example R code
# remove everything in the global environment
rm(list = ls())
n_states = 4 # number of states
v_n <- c("H", "S1", "S2", "D") # the 4 states of the model:
n_t = 100 # number of transitions
# create transition matrix with random numbers. This transition matrix is constant.
m_P = matrix(runif(n_states*n_t), # insert n_states * n_t random numbers (can change this later)
nrow = n_states,
ncol = n_states,
dimnames = list(v_n, v_n))
# create markov trace, what proportion of population in each state at each period (won't make sense due to random numbers but that is fine)
m_TR <- matrix(NA,
nrow = n_t + 1 ,
ncol = n_states,
dimnames = list(0:n_t, v_n)) # create Markov trace (n_t + 1 because R doesn't understand Cycle 0)
# initialize Markov trace
m_TR[1, ] <- c(1, 0, 0, 0)
# run the loop
microbenchmark::microbenchmark( # function from microbenchmark library used to calculate how long this takes to run
for (t in 1:n_t){ # throughout the number of cycles
m_TR[t + 1, ] <- m_TR[t, ] %*% m_P # estimate the Markov trace for cycle the next cycle (t + 1)
}
) # end of micro-benchmark function
print(m_TR) # print the result.
And, here is the replacement for the %*% operator: (WHich doesn't seem to work correctly at all, although I can't fgure out why.
library(Rcpp)
cppFunction(
'void estimate_markov(int n_t, NumericMatrix m_P, NumericMatrix m_TR )
{
// We want to reproduce this
// matrix_A[X+1,] <- matrix_A[X,] %*% matrix_B
// The %*% operation behaves as follows for a vector_V %*% matrix_M
// Here the Matrix M is populated with letters so that you can
// more easily see how the operation is performed
// So, a multiplication like this:
//
// V M
// {1} %*% {A D}
// {2} {B E}
// {3} {C F}
//
// Results in a vector:
// V_result
// {1*A + 1*D}
// {2*B + 2*E}
// {3*C + 3*F}
//
// Now use values instead of letters for M (ex: A=1, B=2, .., F=6)
// V_result
// {1*1 + 1*4} {1 + 4} {5}
// {2*2 + 2*5} => {4 + 10} => {14}
// {3*3 + 3*6} {9 + 18} {27}
//
// Note that the RHS matrix may contain any number of columns,
// but *MUST* must contain the same number of rows as LHS vector
// Get dimensions of matricies , and sanity check
// number of elements in a vector from the LHS matrix must equal == number of row on the RHS
if( m_TR.cols() != m_P.rows())
throw std::range_error("Matrix mismatch, m_P.rows != m_TR.rows");
// we want to know these dimensions, and there is no reason to call these functons in a loop
// store the values once
int cnt_P_cols = m_P.cols();
int cnt_TR_cols = m_TR.cols();
//
for(int Index = 1; Index <= n_t; ++Index)
{
// iterate over the columns in m_TR
for(int col_iter = 0; col_iter < cnt_TR_cols; ++col_iter)
{
// an accumulator for the vector multiplication
double sum = 0;
// The new value comes from the previous row (Index-1)
double orig_TR = m_TR(col_iter, Index-1);
// iterate over the columns in m_P corresponding to this Index
for(int p_iter = 0; p_iter < cnt_P_cols; ++p_iter)
{
// accumulate the value of this TR scalar * the m_P vector
sum += orig_TR * m_P(p_iter, Index);
}
m_TR(col_iter, Index) = sum;
}
}
}'
)
Can someone point me to where my logic is going wrong.

Jags gets wired, returns redefining node errer occasionally, due to different data

I created my code based on this :
http://users.aims.ac.za/~mackay/BUGS/Manual05/Examples1/node29.html
Now I use different seeds to simulate data. It is strange enough that some seeds give me a redefining node dN[1,1] on line 18 error, while others do not. Could someone help please? BTW why is dN[1,1] on line 18 at the first place? How does Jags count lines?
error message :
"
RUNTIME ERROR:
Compilation error on line 18.
Attempt to redefine node dN[1,1]
"
bugsmodel <- "
# Set up data
data{
for(i in 1:N)
{
for(j in 1:bigt)
{
Y[i,j] <- step(obs.t[i] - t[j] + eps)
dN[i, j] <- Y[i, j] * step(t[j + 1] - obs.t[i] - eps) * fail[i]
}
}
}
# Model
model
{
for(i in 1:N){
betax[i,1] <- 0
for(k in 2:(p+1)){
betax[i,k] <- betax[i,k-1] + beta[k-1]*x[i,k-1]
}
}
for(j in 1:bigt) {
for(i in 1:N) {
dN[i, j] ~ dpois(Idt[i, j]) # Likelihood
Idt[i, j] <- Y[i, j] * exp(betax[i,p+1]) * dL0[j] # Intensity
}
dL0[j] ~ dgamma(mu[j], c)
mu[j] <- dL0.star[j] * c # prior mean hazard
}
c <- 0.001
r <- 0.1
for (j in 1 : bigt) {
dL0.star[j] <- r * (t[j + 1] - t[j])
}
for(k in 1:p){
beta[k] ~ dnorm(0.0,0.000001)
}
}"

Longest Common Prefix property

I was going through suffix array and its use to compute longest common prefix of two suffixes.
The source says:
"The lcp between two suffixes is the minimum of the lcp's of all pairs of adjacent suffixes between them on the array"
i.e. lcp(x,y)=min{ lcp(x,x+1),lcp(x+1,x+2),.....,lcp(y-1,y) }
where x and y are two index of the string from where the two suffix of the string starts.
I am not convinced with the statement as in example of string "abca".
lcp(1,4)=1 (considering 1 based indexing)
but if I apply the above equation then
lcp(1,4)=min{lcp(1,2),lcp(2,3),lcp(3,4)}
and I think lcp(1,2)=0.
so the answer must be 0 according to the equation.
Am i getting it wrong somewhere?
I think the index referred by the source is not the index of the string itself, but index of the sorted suffixes.
a
abca
bca
ca
Hence
lcp(1,2) = lcp(a, abca) = 1
lcp(1,4) = min(lcp(1,2), lcp(2,3), lcp(3,4)) = 0
You can't find LCP of any two suffixes by simply calculating the minimum of the lcp's of all pairs of adjacent suffixes between them on the array.
We can calculate the LCPs of any suffixes (i,j)
with the Help of Following :
LCP(suffix i,suffix j)=LCP[RMQ(i + 1; j)]
Also Note (i<j) as LCP (suff i,suff j) may not necessarly equal LCP (Suff j,suff i).
RMQ is Range Minimum Query .
Page 3 of this paper.
Details:
Step 1:
First Calculate LCP of Adjacents /consecutive Suffix Pairs .
n= Length of string.
suffixArray[] is Suffix array.
void calculateadjacentsuffixes(int n)
{
for (int i=0; i<n; ++i) Rank[suffixArray[i]] = i;
Height[0] = 0;
for (int i=0, h=0; i<n; ++i)
{
if (Rank[i] > 0)
{
int j = suffixArray[Rank[i]-1];
while (i + h < n && j + h < n && str[i+h] == str[j+h])
{
h++;
}
Height[Rank[i]] = h;
if (h > 0) h--;
}
}
}
Note: Height[i]=LCPs of (Suffix i-1 ,suffix i) ie. Height array contains LCP of adjacent suffix.
Step 2:
Calculate LCP of Any two suffixes i,j using RMQ concept.
RMQ pre-compute function:
void preprocesses(int N)
{
int i, j;
//initialize M for the intervals with length 1
for (i = 0; i < N; i++)
M[i][0] = i;
//compute values from smaller to bigger intervals
for (j = 1; 1 << j <= N; j++)
{
for (i = 0; i + (1 << j) - 1 < N; i++)
{
if (Height[M[i][j - 1]] < Height[M[i + (1 << (j - 1))][j - 1]])
{
M[i][j] = M[i][j - 1];
}
else
{
M[i][j] = M[i + (1 << (j - 1))][j - 1];
}
}
}
}
Step 3: Calculate LCP between any two Suffixes i,j
int LCP(int i,int j)
{
/*Make sure we send i<j always */
/* By doing this ,it resolve following
suppose ,we send LCP(5,4) then it converts it to LCP(4,5)
*/
if(i>j)
swap(i,j);
/*conformation over*/
if(i==j)
{
return (Length_of_str-suffixArray[i]);
}
else
{
return Height[RMQ(i+1,j)];
//LCP(suffix i,suffix j)=LCPadj[RMQ(i + 1; j)]
//LCPadj=LCP of adjacent suffix =Height.
}
}
Where RMQ function is:
int RMQ(int i,int j)
{
int k=log((double)(j-i+1))/log((double)2);
int vv= j-(1<<k)+1 ;
if(Height[M[i][k]]<=Height[ M[vv][ k] ])
return M[i][k];
else
return M[ vv ][ k];
}
Refer Topcoder tutorials for RMQ.
You can check the complete implementation in C++ at my blog.

How to highlight the differences between subsequent lines in a file?

I do a lot of urgent analysis of large logfile analysis. Often this will require tailing a log and looking for changes.
I'm keen to have a solution that will highlight these changes to make it easier for the eye to track.
I have investigated tools and there doesn't appear to be anything out there that does what I am looking for. I've written some scripts in Perl that do it roughly, but I would like a more complete solution.
Can anyone recommend a tool for this?
Levenshtein distance
Wikipedia:
Levenshtein distance between two strings is minimum number of operations needed to transform one string into the other, where an operation is an insertion, deletion, or substitution of a single character.
public static int LevenshteinDistance(char[] s1, char[] s2) {
int s1p = s1.length, s2p = s2.length;
int[][] num = new int[s1p + 1][s2p + 1];
// fill arrays
for (int i = 0; i <= s1p; i++)
num[i][0] = i;
for (int i = 0; i <= s2p; i++)
num[0][i] = i;
for (int i = 1; i <= s1p; i++)
for (int j = 1; j <= s2p; j++)
num[i][j] = Math.min(Math.min(num[i - 1][j] + 1,
num[i][j - 1] + 1), num[i - 1][j - 1]
+ (s1[i - 1] == s2[j - 1] ? 0 : 1));
return num[s1p][s2p];
}
Sample App in Java
String Diff
Application uses LCS algorithm to concatenate 2 text inputs into 1. Result will contain minimal set of instructions to make one string for the other. Below the instruction concatenated text is displayed.
Download application:
String Diff.jar
Download source:
Diff.java
I wrote a Python script for this purpose that utilizes difflib.SequenceMatcher:
#!/usr/bin/python3
from difflib import SequenceMatcher
from itertools import tee
from sys import stdin
def pairwise(iterable):
"""s -> (s0,s1), (s1,s2), (s2, s3), ...
https://docs.python.org/3/library/itertools.html#itertools-recipes
"""
a, b = tee(iterable)
next(b, None)
return zip(a, b)
def color(c, s):
"""Wrap string s in color c.
Based on http://stackoverflow.com/a/287944/1916449
"""
try:
lookup = {'r':'\033[91m', 'g':'\033[92m', 'b':'\033[1m'}
return lookup[c] + str(s) + '\033[0m'
except KeyError:
return s
def diff(a, b):
"""Returns a list of paired and colored differences between a and b."""
for tag, i, j, k, l in SequenceMatcher(None, a, b).get_opcodes():
if tag == 'equal': yield 2 * [color('w', a[i:j])]
if tag in ('delete', 'replace'): yield color('r', a[i:j]), ''
if tag in ('insert', 'replace'): yield '', color('g', b[k:l])
if __name__ == '__main__':
for a, b in pairwise(stdin):
print(*map(''.join, zip(*diff(a, b))), sep='')
Example input.txt:
108 finished /tmp/ts-out.5KS8bq 0 435.63/429.00/6.29 ./eval.exe -z 30
107 finished /tmp/ts-out.z0tKmX 0 456.10/448.36/7.26 ./eval.exe -z 30
110 finished /tmp/ts-out.wrYCrk 0 0.00/0.00/0.00 tail -n 1
111 finished /tmp/ts-out.HALY18 0 460.65/456.02/4.47 ./eval.exe -z 30
112 finished /tmp/ts-out.6hdkH5 0 292.26/272.98/19.12 ./eval.exe -z 1000
113 finished /tmp/ts-out.eFBgoG 0 837.49/825.82/11.34 ./eval.exe -z 10
Output of cat input.txt | ./linediff.py:
http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html
.. this look promising, will update this with more info when Ive played more..

Resources