samedi 21 novembre 2020

How To Generate A Value Based On Assigned Probability In R?

I am trying to assign individuals to a site. What I want is to make the probability of choice proportional to two location attributes, in this case, square footage and population density. So, choice is a function of sq. footage and population density.

In my model, an individual will be inclined to occupy/select the best site which is a site with the largest sq. footage but may not be able to because of population density. When density is low in a site, individuals will select the best site. When density is high in a site, individuals will select a suboptimal site. I need to somehow incorporate either two probabilities or an if statement but I am not sure which and how to go about it.

Below is what I have so far:

nindiv=100 #create individuals
prob=c(.0,.1,.2,.3,.4) # here I think I need to add an additional probability, not sure

prob=prob/sum(prob)  #normalize prob so it adds to 1
cumprob=c(0,cumsum(prob)) #make a vector that is the boundaries of the probability for each individual

head(cumprob)

#I chose a random number between 0 and 1 for each individual and see where within cumprob this random number falls #so now cumprob is 0.0 0.0 0.1 0.3 0.6 1.0

#if the random number for an individual is less than 0.1 it goes to site 2

#if the random number is less than 0.3 but greater than 0.1 it goes to site 3

#if the random number is less than 0.6 but greater than 0.3 it goes to site 4

#if the random number is less than 1 but greater than 0.6 it goes to site 5

rdraw=runif(nfish,min=0,max=1)  #here is the random number for each individual
choice=array(dim=nfish)  #this will store the section chosen for each individual

for (indiv in 1:nindiv) {
low=which(cumprob<rdraw[indiv] ) #this is a vector of which cumprobs are less than the random number
choice[indiv]=low[length(low)]   #if the random number was 0.05 then low would be  1, 2
                                 #and the site assigned would be 2

 } #end of individual loop

Is there some way to incorporate an interaction between square footage and density that influences an individual's decision to select a site? Is an interaction what I need?




Aucun commentaire:

Enregistrer un commentaire