I am sorry I am being very descriptive here but I hope you could help me with the following problem (I try to program this in R):
Let's say we have array where rows are parties and columns are parties' issue positions (measured as distance from the median issue position across all parties). I want to model p开发者_如何学编程arties announcing an issue platform. This goes like this: start with the issue on which the distance from the median issue position is smallest and announce that platform with probability (1 minus issue distance from median....parties announce that issue that issue as their platform with probability = 1 if they are the median party on that issue). If rbinom(1,1, prob) ==1 they will announce that issue (i.e, the column indicator) as their platform. If rbinom(1,1, prob) == 0, they will move on to the issue on which the distance from the median issue position is second-to-smallest (and draw from a binomial distribution) And so forth until a platform is announced. All parties go through the same steps to find a issue platform for that run of the model, but parties differ on the issues on which they are closest to the median party.
Would you have advice on how to program such a set-up?
I built a toy model that can compute what you want. Assuming there are n parties and k issues, below I provide a code for computing the party choice for one party. It should be fairly straight to generalize the code for all parties. You just need to add a Loop. I left this as an exercise to you =):
n = 4 # example with n=4
k = 3 # k = 3 issues
party_position = matrix(runif(k*n),nrow=n, ncol=k) # matrix with party positions on each issue
med = apply(party_position,2 , median) # compute median of each column
gen.pos = function(party_position, median=med, k) {
if ( k ==1 ) { # case base, i.e., when all previous decisions were rbinom == 0, there is only one platafform left. Pick that one.
issue.announcing = which.max(abs(party_position[1,]-med))
return(issue.announcing)
}
else {
dif=abs(party_position[1,]-med) # difference between party position and median
value=min(dif) # value gets minor difference
pos=which.min(dif) # position in party_position matrix of value
decision = rbinom(1, 1, 1- value) # decision with probability 1 - difference of minimum value and median
if (decision < 1) { # if rbinom < 1, i.e, equals zero
k=k-1 # set new k, so recursive function can work
party_position[1,-pos] # it'll drop of matrix minimum value found before, so we can pick new minimum value
return (gen.pos(party.position, median=med, k)) } # call the function with new matrix
else { #i.e. if decision was equal 1, just pick pos as issue plataform
issue.announcing = pos
return (issue.announcing)
}
}
}
The functon "gen.pos" will find the party plataform for party one (row one). I guess you just need to apply a "for" to generate positions for all parties. Note that the function is recursive, which is, btw, the reason why I spent my time into this: I really like to write recursive functions!
ps.: Check my function. It seemed to work here and I think it's correct, but as some people say, 'trust, but check'.
ps.2: the function returns the position (i.e., the column) for party one. If you need the number, not the position, juts use,
position.final = gen.pos(Party_position, med, k)
plataform = party_position[1,position.final]
精彩评论