From Excel to R

Viewed 70

I have data frame and want to calculate the AR by manual in R not by the package:

mydata <- data.frame(gender = c("Female", "Male", "Male", "Male", 
                                "Female", "Male", "Female", "Male", "Male"),
                     flag = c(1,0,1,0,1,1,0,1,0))

mydata %>% 
  arrange(desc(gender))

enter image description here

I have done it with excel but i always fail to run it use R

# Cumulative % pop
random = 1:length(mydata$pred)/length(mydata$pred)

# Cumulative % of Bads
cumpercentbad = cumsum(mydata$flag)/sum(mydata$flag)
# Calculate AR
random = c(0,random)
cumpercentbad = c(0,cumpercentbad)
idx = 2:length(cumpercentbad)
testdf=data.frame(cumpercentpop = (random[idx] - random[idx-1]), 
                  cumpercentbad = (cumpercentbad[idx] + cumpercentbad[idx-1]))
Area = sum(testdf$cumpercentbad * testdf$cumpercentpop/2)
Numerator = Area - 0.5
p(Bad) = sum(mydata$flag)/length(mydata$flag)
Denominator = 0.5*(1-Bad)
(AR = Numerator / Denominator)

The result from this 20% and I try another one variable, it gives different result

0 Answers
Related