I am trying to find the probability that the chain jumps from state k-1 to state 1 before it hits state k.
Can anyone spot my mistake?
I tried to simulate the markov chain, but i want to make a code that allows me to find probability of k ={1, 2, 3, ........17}. But I can really not get the code.
This is the error message I always get
Error in while (X[i] > 1 && X[i] < k) { :
missing value where TRUE/FALSE needed
Here is my code:
k <- 17
{ p <- 0.5
q <- 0.1
P <- matrix (0, nrow = k, ncol = k, byrow = TRUE)
for (i in 1:k)
{ for (j in 1:k)
{ if (i == 1 && i == j)
{ P[i,j] <- 1
}
else if (i == k && i == j)
{ P[i,j] <- 1
}
else if (i == j)
{ P[i,j] <- p*(1-q)
}
else if (j == k && i != 1)
{ P[i,j] <- q
}
else if (i == j+1 && i != k)
{ P[i,j] <- (1-p)*(1-q)
}
}
}
P
X <- (k-1)
trials <- 1000
hits <- 0 #counter for no. of hits
for (i in 1:trials)
{ i <- 1 #no. of steps
while(X[i] > 1 && X[i] < k)
{ Y <- runif(1) #uniform samples
p1 <- P[X[i],] #calculating the p-value
p1 <- cumsum(p1)
# changes in the chain
if(Y <= p1[1])
{ X[i+1] = 1}
else if(Y <= p1[2])
{ X[i+1] = 2}
else if(Y <= p1[3])
{ X[i+1] = 3}
else if(Y <= p1[4])
{ X[i+1] = 4}
else if(Y <= p1[5])
{ X[i+1] = 5}
else if(Y <= p1[6])
{ X[i+1] = 6}
else if(Y <= p1[7])
{ X[i+1] = 7}
else if(Y <= p1[8])
{ X[i+1] = 8}
else if(Y <= p1[9])
{ X[i+1] = 9}
else if(Y <= p1[10])
{ X[i+1] = 10}
else if(Y <= p1[11])
{ X[i+1] = 11}
else if(Y <= p1[12])
{ X[i+1] = 12}
else if(Y <= p1[13])
{ X[i+1] = 13}
else if(Y <= p1[14])
{ X[i+1] = 14}
else if(Y <= p1[15])
{ X[i+1] = 15}
else if(Y <= p1[16])
{ X[i+1] = 16}
else if(Y <= p1[17])
{ X[i+1] <= 17}
i <- i+1
}
if(X[i]==1)
{ hits <- hits+1}
else
{ hits <- hits+0}
}
Probability <- hits/trials
Probability
}
I think the line
i <- 1 #no. of steps
should not be there. Try this:
k <- 17
{ p <- 0.5
q <- 0.1
P <- matrix (0, nrow = k, ncol = k, byrow = TRUE)
for (i in 1:k)
{ for (j in 1:k)
{ if (i == 1 && i == j)
{ P[i,j] <- 1
}
else if (i == k && i == j)
{ P[i,j] <- 1
}
else if (i == j)
{ P[i,j] <- p*(1-q)
}
else if (j == k && i != 1)
{ P[i,j] <- q
}
else if (i == j+1 && i != k)
{ P[i,j] <- (1-p)*(1-q)
}
}
}
P
X <- (k-1)
trials <- 1000
hits <- 0 #counter for no. of hits
for (i in 1:trials)
{
while(X[i] > 1 && X[i] < k)
{ Y <- runif(1) #uniform samples
p1 <- P[X[i],] #calculating the p-value
p1 <- cumsum(p1)
# changes in the chain
if(Y <= p1[1])
{ X[i+1] = 1}
else if(Y <= p1[2])
{ X[i+1] = 2}
else if(Y <= p1[3])
{ X[i+1] = 3}
else if(Y <= p1[4])
{ X[i+1] = 4}
else if(Y <= p1[5])
{ X[i+1] = 5}
else if(Y <= p1[6])
{ X[i+1] = 6}
else if(Y <= p1[7])
{ X[i+1] = 7}
else if(Y <= p1[8])
{ X[i+1] = 8}
else if(Y <= p1[9])
{ X[i+1] = 9}
else if(Y <= p1[10])
{ X[i+1] = 10}
else if(Y <= p1[11])
{ X[i+1] = 11}
else if(Y <= p1[12])
{ X[i+1] = 12}
else if(Y <= p1[13])
{ X[i+1] = 13}
else if(Y <= p1[14])
{ X[i+1] = 14}
else if(Y <= p1[15])
{ X[i+1] = 15}
else if(Y <= p1[16])
{ X[i+1] = 16}
else if(Y <= p1[17])
{ X[i+1] <= 17}
i <- i+1
}
if(X[i]==1)
{ hits <- hits+1}
else
{ hits <- hits+0}
}
Probability <- hits/trials
Probability
}
You're setting X to k-1. In R, that's treated as a vector of length 1. As soon as i reaches 2, X[i] return an index error, because X does not have a second element.
Further notes: using the same index in two different nesting levels is very bad form. Also, when you start having a massive list of if-then-else statements, it's time to rethink your code. In this case, you could just subset 1:17 on p1[i] >=Y, take the minimum value, and then set X to that.
I would like to make in JavaFX a 9x9 sudoku grid like in this image
Any idea how to do it in a nice way? Thanks
Edit:
I managed to do it, but the code doesn't look so good.
private void addBackground(StackPane cell, int row, int col) {
String[] colors = {"#b0cbe1", "#cbe183", "#e18398","#b0e183", "#b8778a", "#e198b0", "#b08398", "#cb98b0", "#e1b0cb"};
if(row < 3) {
if(col < 3) {
cell.setStyle("-fx-background-color: " + colors[0] + ";");
} else if (col >= 3 && col < 6 ) {
cell.setStyle("-fx-background-color: " + colors[1] + ";");
} else{
cell.setStyle("-fx-background-color: " + colors[2] + ";");
}
} else if (row >= 3 && row <6) {
if(col < 3) {
cell.setStyle("-fx-background-color: " + colors[3] + ";");
} else if (col >= 3 && col < 6 ) {
cell.setStyle("-fx-background-color: " + colors[4] + ";");
} else {
cell.setStyle("-fx-background-color: " + colors[5] + ";");
}
} else {
if(col < 3) {
cell.setStyle("-fx-background-color: " + colors[6] + ";");
} else if (col >= 3 && col < 6 ) {
cell.setStyle("-fx-background-color: " + colors[7] + ";");
} else{
cell.setStyle("-fx-background-color: " + colors[8] + ";");
}
}
}
Instead of all those if-else constructs you can use
int colorIndex = 3 * (row / 3) + (col / 3);
cell.setStyle("-fx-background-color: " + colors[colorIndex] + ";");
Note that row / 3 is calculated using integer division, so row / 3 is 0 when row is in {0, 1, 2}, 1 when row is in {3, 4, 5}, and 2 when row is in {6, 7, 8}. (So 3 * (row / 3) is not equal to row. )
I have a problem outputting my result correctly when I apply my own function in ddply.
Basically, I wrote a function that will output 2 variables (aggressive_name, aggressive_node). But when I run this function in ddply, the function values are not output.
option_1_aggressive <- function(first_date,Market) {
if (Market =="Automotive" && first_date < yearqtr(2017 + 0)) { aggressive_name = "PC2E" aggressive_node="19nm"}
else if (Market =="Automotive" && first_date >= yearqtr(2017 + 0)) {aggressive_name = "Osprey_BiCS_Auto" aggressive_node="BiCS2"}
else if (Market !="Automotive" && first_date < yearqtr(2014 + 0.25)) { aggressive_name = "PC2E" aggressive_node="19nm"}
else if (Market !="Automotive" && yearqtr(2014 + 0.25) <= first_date && first_date<= yearqtr(2014 + 0.75)) { aggressive_name = "Whale" aggressive_node="1Ynm"}
else if (Market !="Automotive" && yearqtr(2015 + 0) <= first_date && first_date<= yearqtr(2015 + 0.25) ) {aggressive_name = "Robin/Seagull" aggressive_node="1Ynm"}
else if (Market !="Automotive" && yearqtr(2015 + 0.5) <= first_date && first_date <= yearqtr(2016 + 0.5) ) {aggressive_name = "Z-hawk" aggressive_node="1Znm" }
else if (Market !="Automotive" && yearqtr(2016 + 0.75) <= first_date && first_date<= yearqtr(2017 + 0.75) ) {aggressive_name = "Osprey_BiCS_Mobile" aggressive_node="BiCS2"}
else if (Market !="Automotive" && yearqtr(2018 + 0) <= first_date ) {aggressive_name = "PCIe" aggressive_node="BiCS3"}
result <-list(aggressive_name , aggressive_node)
}
option_1_aggressive <- ddply(combine, .(Market, Customer, Product_Line, Platform_Name, Die_Name, Controller, Capacity, first_date), summarise, option_1_aggressive(first_date,Market))
FURTHER UPDATE
There's no input, because the program creates data.
The code below works for the first two patients, but I want to put it in a loop; there will be 200 patients eventually. Sorry about length.
s <- 42 #SHOULD BE 42
p <- 30 #SHOULD BE 400
p1 <- 8 #Should be 8
m <- 2 #shou;ld be higher
w <- 2
#CAPITAL LETTERS are counting versions of the lowercase
numses <- 12
mult <- 1
maxses <- round(mult*numses,0)
drop = .3; titrate = .2; complete = .3; noise = .5; other = .1
dropskip = .3; titrateskip = .2; completeskip = .3; noiseskip = .5; otherskip = .1;
dropnew = .3; titratenew = .2; completenew = .9; noisenew = .5; othernew = .1;
name = "Basic";
pdrop = .1; ptitrate = .2; pcomplete =.7; pnoise = .9; pother = 1;
# THESE HAVE TO BE ASCENDING.
w = 10
######################################################################################################################
patients <- matrix(nrow = p, ncol = s, "NA") #all patients attendance
invited <- matrix(nrow = p, ncol = s, 0) #number of sessions A or S
missinrow <- matrix(nrow = p, ncol = s, 0) #number of sessions missed in a row
insess <- vector("numeric", s) #number of people in a session
set.seed(83877201)
addlist <- rpois(s, w)
# Set up waitlist
waitlist <- vector("numeric", s)
waitlist[1] <- addlist[1]
for(i in 2:s)
{
waitlist[i] <- waitlist[i-1] + addlist[i]
}
for (i in 1:p)
{
for (j in 1:s)
{
if (i < waitlist[j]) patients[i,j] <- "W"
}
}
#Assign all patients to classes
classlist <- cut(runif(p), c(0, pdrop, ptitrate, pnoise, pcomplete, 1), labels = c("D", "T", "C", "N", "O"))
#First patient
#first session
{
invited[1,1] <- 1
insess[1] <- 1
if (classlist[1] == "D")
{
if (runif(1) < dropnew) {patients[1, 1] <- 'A'} else
{
patients[1, 1] <- 'S'
missinrow[1, 1] <- 1
}
}
if (classlist[1] == "T")
{
if (runif(1) < titratenew) {patients[1, 1] <- 'A'} else
{
patients[1, 1] <- 'S'
missinrow[1, 1] <- 1
}
}
if (classlist[1] == "C")
{
if (runif(1) < completenew) {patients[1, 1] <- 'A'} else
{
patients[1, 1] <- 'S'
missinrow[1, 1] <- 1
}
}
if (classlist[1] == "N")
{
if (runif(1) < noisenew) {patients[1, 1] <- 'A'} else
{
patients[1, 1] <- 'S'
missinrow[1, 1] <- 1
}
}
if (classlist[1] == "O")
{
if (runif(1) < othernew) {patients[1, 1] <- 'A'} else
{
patients[1, 1] <- 'S'
missinrow[1, 1] <- 1
}
}
}
#Later sessions
for (j in 2 : s)
{
if (patients[1,(j-1)] == 'A'|patients[1,(j-1)] == 'S')
{
invited[1,j] <- invited[1,(j-1)] + 1
} else
{
invited[1,j] <- invited[1,(j-1)]
}
if (invited[1,j] <= maxses & missinrow[1,j] < m)
{
# Skip or attend
# If attended previous session
if (patients[1, (j-1)] == 'A')
{
if (classlist[1] == "D")
{if (runif(1) < drop) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- 1
}
}
if (classlist[1] == "T")
{if (runif(1) < titrate) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- 1
}
}
if (classlist[1] == "C")
{if (runif(1) < complete) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- 1
}
}
if (classlist[1] == "N")
{
if (runif(1) < noise) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- 1
}
}
if (classlist[1] == "O")
{
if (runif(1) < other) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- 1
}
}
} else
# If skipped previous session
if (patients[1, (j-1)] == 'S')
{
if (classlist[1] == "D")
{
if (runif(1) < drop) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- missinrow[1, (j-1)] + 1
}
}
if (classlist[1] == "T")
{
if (runif(1) < titrate) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- missinrow[1, (j-1)] + 1
}
}
if (classlist[1] == "C")
{if (runif(1) < complete) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- missinrow[1, (j-1)] + 1
}
}
if (classlist[1] == "N")
{if (runif(1) < noise) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- missinrow[1, (j-1)] + 1
}
}
if (classlist[1] == "O")
{
if (runif(1) < other) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- missinrow[1, (j-1)] + 1
}
}
}
} else {patients[1,j] <- 'D'}
# check for number of attended or missed sessions
if (patients[1,(j-1)] == 'A' | patients[1,(j-1)] == 'S')
{
insess[j] <- 1
} else
{
insess[j] <- 0
}
}
#Second patients
#Patient is waiting and there is space
#First session
if (insess[1] < maxses & waitlist[1] > 0) #THIS MAY NEED TO BE A WHILE LOOP, FOR MULTIPLE PATIENTS
{
invited[2,1] <- 1
insess[1] <- insess[1] + 1
if (classlist[2] == "D")
{
if (runif(1) < dropnew) {patients[1, 1] <- 'A'} else
{
patients[2, 1] <- 'S'
missinrow[2, 1] <- 1
}
}
if (classlist[2] == "T")
{
if (runif(1) < titratenew) {patients[2, 1] <- 'A'} else
{
patients[2, 1] <- 'S'
missinrow[2, 1] <- 1
}
}
if (classlist[2] == "C")
{
if (runif(1) < completenew) {patients[2, 1] <- 'A'} else
{
patients[2, 1] <- 'S'
missinrow[2, 1] <- 1
}
}
if (classlist[2] == "N")
{
if (runif(1) < noisenew) {patients[2, 1] <- 'A'} else
{
patients[2, 1] <- 'S'
missinrow[2, 1] <- 1
}
}
if (classlist[2] == "O")
{
if (runif(1) < othernew) {patients[2, 1] <- 'A'} else
{
patients[2, 1] <- 'S'
missinrow[2, 1] <- 1
}
}
}
#Later sessions
#Patient invited previous session
for (j in 2 : s)
{
if (patients[2,(j-1)] == 'A'|patients[2,(j-1)] == 'S')
{
invited[2,j] <- invited[2,(j-1)] + 1
} else
{
invited[2,j] <- invited[2,(j-1)]
}
if (invited[2,j] <= maxses & missinrow[2,j] < m)
{
# Skip or attend
# If attended previous session
if (patients[2, (j-1)] == 'A')
{
if (classlist[2] == "D")
{if (runif(1) < drop) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- 1
}
}
if (classlist[2] == "T")
{if (runif(1) < titrate) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- 1
}
}
if (classlist[2] == "C")
{if (runif(1) < complete) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- 1
}
}
if (classlist[2] == "N")
{
if (runif(1) < noise) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- 1
}
}
if (classlist[2] == "O")
{
if (runif(1) < other) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- 1
}
}
} else
# If skipped previous session
if (patients[2, (j-1)] == 'S')
{
if (classlist[2] == "D")
{
if (runif(1) < drop) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- missinrow[2, (j-1)] + 1
}
}
if (classlist[2] == "T")
{
if (runif(1) < titrate) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- missinrow[2, (j-1)] + 1
}
}
if (classlist[2] == "C")
{if (runif(1) < complete) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- missinrow[2, (j-1)] + 1
}
}
if (classlist[2] == "N")
{if (runif(1) < noise) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- missinrow[2, (j-1)] + 1
}
}
if (classlist[2] == "O")
{
if (runif(1) < other) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- missinrow[2, (j-1)] + 1
}
}
}
} else {patients[2,j] <- 'D'}
# check for number of attended or missed sessions
if (patients[2,(j-1)] == 'A' | patients[2,(j-1)] == 'S')
{
insess[j] <- insess[j] + 1
} else
{
insess[j] <- insess[j]
}
}
UPDATED with additional information
Thanks, Joshua, for your help so far.
Joshua asked for additional information in order to be able to help further.
The overall problem is to create a dataset of patients who attend different sessions of therapy. Whether they attend or not depends on a number of parameters, including whether they attended the previous session (which is in patients), what "class" of patient they are (which is in classlist), how many sessions they have already either attended or skipped (invited) and how many sessions in a row they have missed (missinrow); all of those will vary. If the previous session is less than full, additional patients get assigned to the new session (provided someone is waiting (waitlist).
When I do this one patient at a time, it works well. But when I try to loop over all patients, I run into the
Hello
This is one part of a larger program. I cannot figure out why it doesn't work as I thought it would. The code is
for(i in 2:p)
{
while (insess[1] < maxses & waitlist[1] > 0)
{
invited[i,1] <- 1
waitlist[1] <- waitlist[1] - 1
insess[1] <- insess[1] + 1
if (classlist[i] == "D")
{
if (runif(1) < dropnew) {patients[i, 1] <- 'A'} else
{
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
if (classlist[i] == "T")
{
if (runif(1) < titratenew) {patients[i, 1] <- 'A'} else
{
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
if (classlist[i] == "C")
{
if (runif(1) < completenew) {patients[i, 1] <- 'A'} else
{
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
if (classlist[i] == "N")
{
if (runif(1) < noisenew) {patients[i, 1] <- 'A'} else
{
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
if (classlist[i] == "O")
{
if (runif(1) < othernew) {patients[i, 1] <- 'A'} else
{
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
}
}
When I run this, i goes to 30 (which is right), insess[1] goes to 10 (also right), waitlist[1] goes to 0 (also right) but the patients matrix is only updated for i = 2. I also checked that classlist has appropriate values.
I don't see where it is leaving the loop. I think I'm using while incorrectly, but not sure how.
thanks!
You're not resetting insess[1] and waitlist[1] in each iteration of the for loop. When i = 3, insess[1] = 10 and waitlist[1] = 0, so nothing in the while loop is executed.
As as added bonus, here's a more succinct version of your code using the switch function.
for(i in 2:p)
{
insess[1] <- INITME
waitlist[1] <- INITMETOO
while (insess[1] < maxses & waitlist[1] > 0)
{
invited[i,1] <- 1
waitlist[1] <- waitlist[1] - 1
insess[1] <- insess[1] + 1
newswitch <-
switch(classlist[i],
D = dropnew,
T = titratenew,
C = completenew,
N = noisenew,
O = othernew)
if (runif(1) < newswitch) {
patients[i, 1] <- 'A'
} else {
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
}