Please see below my code for trying to run a loop of 10000 iterations, every time the code loops I want it to select a new value for my two random variables labelled: premium_A_1 and cost_of_claim.
for (i in 1:10000){
profit_A_scheme1 = c()
premium_A_scheme1=sample(c(200,170,140), size = 1, replace = TRUE, prob = s.d_scheme1)
costclaim_A= runif(1, 0, 400)
profit_A_scheme1[i] = premium_A_scheme1 - costclaim_A
}
The code returns profit_A_scheme_1 = (NA, NA, ..., x) when I was hoping for profit_A_scheme_1 = (x1, x2, ..., xn). Essentially only assigning a value to the final loop and NA for every loop previous. In case anyone tries to run this code the probabilities for the premium r.v. are prob = (0.4510610, 0.3207926, 1 - 0.4510620 - 0.3207926).
Thanks for any help you are able to offer as I've been stuck on this for a minute now.
Try (replace prob =abs(rnorm(3)/100) with your own) :
for (i in 1:10000){
profit_A_scheme1 = NULL
premium_A_scheme1=sample(c(200,170,140), size = 1, replace = TRUE, prob =abs(rnorm(3)/100))
costclaim_A= runif(1, 0, 400)
profit_A_scheme1 = premium_A_scheme1 - costclaim_A
print(profit_A_scheme1)
}
Related
I am trying to generate a for loop that will repeat a sequence of the following:
sample(x = 1:14, size = 10, replace = TRUE, prob = c(1/4,1/4,1/4,1/4)
I want it to repeat 5000 times. So far, I include the above as the body of the loop and added
for (i in seq_along[1:5000]){
at the beginning but I am getting an error message saying
Error in seq_along[1:10000] : object of type 'builtin' is not subsettable
We need replicate
out <- replicate(5000, sample(x = 1:14, size = 10, replace = TRUE, prob = c(1/4,1/4,1/4,1/4)), simplify = FALSE)
There are a few issues here.
#MartinGal noted the syntax issues with seq_along and the missing ). Note that you can use seq(n) or 1:n in defining the number of loops.
You are not storing the sampled vectors anywhere, so the for loop will run the code but you won't capture the output.
You have x = 1:14 but you only have 4 prob values, which suggests you intended x = 1:4 (either that or you are 10 prob values short).
Here's one way to address these issues using a for loop.
n <- 5
s <- 10
xmax <- 4
p <- 1/4
out <- matrix(nrow = n, ncol = s, byrow = TRUE)
set.seed(1L)
for (i in seq(n)) {
out[i, ] <- sample(x = seq(xmax), size = s, replace = TRUE, prob = rep(p, xmax))
}
As andrew reece notes in his comment, it looks like you want x = 1:4 Depending what you want to do with your result you could generate all of the realizations at one time since you are sampling with replacement and then store the result in a matrix with 5000 rows of 10 realizations per row. So:
x <- sample(1:4, size = 5000 * 10, replace = TRUE, prob = c(1/4,1/4,1/4,1/4))
result <- matrix(x, nrow = 5000)
I am trying to set up a process using a while loop in order to have my code consistently sample among certain xd[i] before one particular xd[i] becomes equal to x.
I know it would be more efficient to put everything under one for loop (except for the while loop) but I am trying to create this step by step. Right now, I am stuck on the while loop part. I cannot run that part of the code without R crashing, or if it does not crash, it seems to continue sampling nonstop until I manually stop it. How can I change my while loop such that it samples over the xd vector until one of the elements of xd matches with x?
Thank you
reset1 = {
a = 0.3 #lower legal threshold
b = 0.9 #upper legal threshold
x = 0
theta = runif(1,min = a, max = b)
theta
A = 5 ## monetary value of harm from
maxw = 2*A
minw = 0
wbar = (maxw+minw)/2 ##average cost
wbar
xd = c(1,2,3)
w = c(1,2,3)
}
for (i in 1:length(xd)){w[i] = runif(1, min = 0, max = 2)} #trying to make it create a w for each person
##Drivers problem: pick the x that will minimize your cost
for(i in 1:length(xd)){xd[i] = min(c(1-(w[i]/(2*A)),((2+b)-sqrt(b^2-2*b+1+3*(w[i]/A)*(b-a)))/3,b))}
xd
for(i in 1:length(xd)){proba = function(xd){(xd-1)^2}}
proba(xd) #ith individual probability of getting in an accident given their xd[i]
proba(xd[c(1:3)])
probn = 1 - proba(xd) #probability of not getting in an accident given driveri's effort level
probn
while (any(x!=xd)) {x = sample(c(xd[c(1,2,3)],0,0,0),size = 1, replace = TRUE, prob = c(proba(xd), probn)) ###the x is selected based on which ever x resulted in an accident
}
show(x)
Perhaps
while(sum(xd!=x)==3){}
This loops runs as long as no element of xd equals x
I am repeatetly drawing large matrices with random values from a Monte Carlo Simulation. As I explore a large parameter space, the simulation will most likely run for several days, therefore I am trying to find most efficient way to shave off as much time as possible. Consider the following code with a 500x18 Matrix as an example.
U = matrix(sample.int(500, size = 500*18, replace = TRUE), nrow = 500, ncol = 18)
X = matrix(nrow= 500, ncol = 18)
Marginals = matrix(runif(500*18, min = 0, max = 1),500,18)
for (i in 1:18){
for (k in 1:500){
X[k,i] = Marginals[U[k,i],i]
}
}
The randomly drawn values in U serve as the row index, while the col index is provided by column of the respective U.
I know that loops are usually not the R away, is there a more efficient way to use e.g. apply here?
By Yogos Suggesiton, the most efficient code can make due without the k loop:
U = matrix(sample.int(500, size = 500*18, replace = TRUE), nrow = 500, ncol = 18)
X = matrix(nrow= 500, ncol = 18)
Marginals = matrix(runif(500*18, min = 0, max = 1),500,18)
for (i in 1:18){
X[, i] <- Marginals[U[, i], i]
}
You can speed up by calculating column by column:
for (i in 1:18) X[, i] <- Marginals[U[, i], i]
Eventually the following is equivalent to your code:
X <- replicate(18, sample(runif(500), repl=TRUE))
(this will not be much faster than my first variant, but the code is more compact)
I am working with the genalg library for R, and try to save all the generations when I run a binary generic algorithm. It does not seems like there is a built-in method for that in the library, so my attempt was to save each chromosome, x, coming through the evaluation function.
To test this method I have tried to insert print(x) in the evaluation function to be able to see all the evaluated chromosomes. However, the number of printed chromosomes does not always match what I am suspecting.
I thought that the number of printed chromosomes would be equal to the number of iterations times the population size, but it does not seems to be try all the time.
The problem is that I want to know from which generation (or iteration) each chromosome belongs, which I can't tell if the number of chromosomes are different from iter times popSize.
What is the reason for this, and how can I "fix" it. Or is there another way of saving each chromosome and from which iteration it belongs?
Below is an example, where I thought that the evaluation function would print 2x5 chromosomes, but only prints 8.
library(genalg)
library(ggplot2)
dataset <- data.frame(
item = c("pocketknife", "beans", "potatoes", "unions", "sleeping bag", "rope", "compass"),
survivalpoints = c(10, 20, 15, 2, 30, 10, 30),
weight = c(1, 5, 10, 1, 7, 5, 1))
weightlimit <- 20
evalFunc <- function(x) {
print(x)
current_solution_survivalpoints <- x %*% dataset$survivalpoints
current_solution_weight <- x %*% dataset$weight
if (current_solution_weight > weightlimit)
return(0) else return(-current_solution_survivalpoints
}
iter = 2
popSize = 5
set.seed(1)
GAmodel <- rbga.bin(size = 7, popSize = popSize, iters = iter, mutationChance = 0.1,elitism = T, evalFunc = evalFunc)
Looking at the function code, it seems like at each iteration (generation) a subset of chromosomes is chosen from the population (population = 5 chromosomes in your example) with a certain probability (0.1 in your case) and mutated. Evaluation function is called only for the mutated chromosomes at each generation (and of course for all the chromosomes in the first iteration to know their initial value).
Note that, this subset do not include elitists group, which in your example you have defined as 1 element big (you have erroneously passed elitism=TRUE and TRUE is implicitly converted to 1).
Anyway, to know the population at each generation, you can pass a monitor function through the monitorFun parameter e.g. :
# obj contains a lot of informations, try to print it
monitor <- function(obj) {
print(paste(" GENERATION :", obj$iter))
print("POPULATION:")
print(obj$population)
print("VALUES:")
print(obj$evaluations)
}
iter = 2
popSize = 5
set.seed(1)
GAmodel <- rbga.bin(size = 7, popSize = popSize,
iters = iter, mutationChance = 0.1,
elitism = 1, evalFunc = evalFunc, monitorFunc = monitor)
Suppose I have a vector of probabilities that sum to 1, such as foo = c(0.2,0.5,0.3).
I would like to sample an index from this vector by treating the values as probabilities.
In particular, I'd like to sample 1 with probability 0.2, 2 with probability 0.5, and 3 with probability 0.3.
Here is one implementation, similar to what I would write in C:
sample_index = function(probs) {
r = runif(1)
sum = 0
for (i in 1:length(probs)) {
sum <- sum + probs[i]
if (r < sum) return(i)
}
}
foo = c(0.2,0.5,0.3)
print(sample_index(foo));
Is there a more direct / built-in / canonical way to do this in R?
It always makes me smile and think R is doing a good job when people are looking for a function and repeatedly use its name in their question.
foo <- c(0.2, 0.5, 0.3)
sample(x = 1:3, size = 1, prob = foo)
Depending on your use case, you could make it a little more general:
sample(x = seq_along(foo), size = 1, prob = foo)
But do be careful, sample has sometimes convenient but very often unexpected behavior if its x argument is of length 1. If you're wrapping this up in a function, check the input length
if (length(foo) == 1) foo else sample(x = seq_along(foo), size = 1, prob = foo)