Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In a previous blog post I discussed how we can compute rank dependent utility in R. Though this function was quite exciting for myself as computing rank dependent utilty by hand is quite labor intensive, I saw the function as limiting in the sense that I was suppressing all the information generated in the process of computing rank dependent utility. This inspired me to modify the original `RDU_Compute()` to `RDU_Data()`. Lets see some of the features and output of this newer function.

### Getting more with RDU_Data()

The function `RDU_Data()` is built as follows:

```RDU_Data<-function(outcomes,p_vec,pw){

if(length(outcomes)==length(p_vec)){
#Step 0 organize outcomes and probabilities into a single vector
df<-data.frame(outcomes,p_vec)

#Step 1 Organize Probabilities by outcome.
df1<-df[order(-outcomes),]
zerorow<-c(0,0)
df2<-rbind(zerorow,df1)

#Step 2  Define vector of ranks
rank<-cumsum(df2[,2])
df3<-data.frame(df2,rank)

#Step 3 compute pweights
pw_vec<-pw(df3\$rank)
df4<-data.frame(df3,pw_vec)

#Step 4 Take difference between pw_vec to compute decision weights
d_weights<-diff(df4\$pw_vec)
#Check if weights sum to 1
sum(d_weights)
d_weights1<-c(0,d_weights)
df5<-data.frame(df4,d_weights1)

#Step 5 drop the first "helper" row
df6<-df5[-1,]
colnames(df6)<-c("Outcomes","Probabilities","Rank",
"Weighted Ranks", "Decision Weights")
return(df6)} else{
print("Outcomes and vector of probabilities must be the same length")
}
}

```

After generating this function we can apply it to valuing a set of uncertain outcomes defined by a vector of outcomes and a corresponding vector of probabilities.

```#Define outcomes, probabilities and probability weighting function.

payoffs<-c(2,1,-1)
probs<-c(1/3,1/2,1/6)
p_0.61<-function(p){p^0.61/((p^0.61+(1-p)^0.61)^(1/0.61))}

#Compute results
RDU_Data(payoffs,probs,p_0.61)

#Output:
Outcomes Probabilities      Rank Weighted Ranks Decision Weights
2        2     0.3333333 0.3333333      0.3359522       0.33595216
3        1     0.1666667 0.5000000      0.4206394       0.08468719
4       -1     0.5000000 1.0000000      1.0000000       0.57936065
```

This framework is desirable when you want to get information on your decision weights and more information on how its computed. You can easily compute the rank dependent utility from this valuation by running the following code:

```#Computing Rank Dependent Utility
payoffs%*%RDU_Data(payoffs,probs,p_0.61)\$`Decision Weights`

#Output
[,1]
[1,] 0.1772309
```

### Conclusion

The goal of this R function was to make the computing and valuing of uncertain outcomes under rank dependent utility theory accessible and easy to implement, while giving all the “behind the scenes” information on what goes into the valuation. Perhaps I’ll think about writing an R package if I keep working on these types of models. We’ll see.