Skip to content

Instantly share code, notes, and snippets.

@statcompute
Last active May 28, 2020 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statcompute/3ceda74ade266bb9d1ee89115ba21833 to your computer and use it in GitHub Desktop.
Save statcompute/3ceda74ade266bb9d1ee89115ba21833 to your computer and use it in GitHub Desktop.
df1 <- read.table("credit_count.txt", header = T, sep = ",")
df2 <- df1[which(df1$CARDHLDR == 1), ]
Y <- df2$DEFAULT
X <- scale(df2[, 3:ncol(df2)])
i <- sample(seq(length(Y)), length(Y) / 2)
# WITHOUT BINNING
Y1 <- Y[i]
Y2 <- Y[-i]
X1 <- X[i, ]
X2 <- X[-i, ]
net11 <- grnn.fit(x = X1, y = Y1)
test1 <- grnn.search_auc(net11, gen_latin(1, 3, 10), nfolds = 4)
# $best
# sigma auc
# 2.198381 0.6297201
net12 <- grnn.fit(x = X1, y = Y1, sigma = test1$best$sigma)
MLmetrics::AUC(grnn.parpred(net12, X1), Y1)
# 0.6855638
MLmetrics::AUC(grnn.parpred(net12, X2), Y2)
# 0.6555798
# WITH BINNING
df3 <- data.frame(df2[, 3:ncol(df2)], Y)
bin_out <- batch_bin(df3, method = 3)
df_woe <- batch_woe(df3, bin_out$BinLst)
W <- scale(df_woe$df[, -1])
W1 <- W[i, ]
W2 <- W[-i, ]
net21 <- grnn.fit(x = W1, y = Y1)
test2 <- grnn.search_auc(net21, gen_latin(1, 3, 10), nfolds = 4)
# $best
# sigma auc
# 2.198381 0.6820317
net22 <- grnn.fit(x = W1, y = Y1, sigma = test2$best$sigma)
MLmetrics::AUC(grnn.parpred(net22, W1), Y1)
# 0.7150051
MLmetrics::AUC(grnn.parpred(net22, W2), Y2)
# 0.6884229
@jasepark2
Copy link

I found your excellent artile from "https://statcompute.wordpress.com/2012/12/30/modeling-in-r-with-log-likelihood-function/" but I have trouble to find the "credit_count.csv" for replication. Could you let me have the file or link for downloading please?

@statcompute
Copy link
Author

statcompute commented May 27, 2020 via email

@jasepark2
Copy link

jasepark2 commented May 28, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment