Skip to content

Instantly share code, notes, and snippets.

@howardjp
Created November 26, 2018 14:59
Show Gist options
  • Save howardjp/01d2039e350b9581ccf2bba4011347da to your computer and use it in GitHub Desktop.
Save howardjp/01d2039e350b9581ccf2bba4011347da to your computer and use it in GitHub Desktop.
This is a mostly ggplot-ish implementation of the plot for dbchoice and sbchoice models in the DCchoice package.
### This is a mostly ggplot-ish implementation of the plot for dbchoice
### and sbchoice models in the DCchoice package.
ggplot.dbchoice <- function(x, ...) {
minbid <- min(x$bid)
maxbid <- max(x$bid)
bidseq <- seq(minbid, maxbid, by = (maxbid - minbid) / 100)
b <- x$coefficients
npar <- length(b)
Xcov <- colMeans(x$covariates)
Vcov <- sum(Xcov * b[-npar])
V <- Vcov + bidseq * b[npar]
dist <- x$distribution
if (dist == "logistic" | dist == "log-logistic") {
pr <- plogis(-V, lower.tail = FALSE, log.p = FALSE)
} else if (dist == "normal" | dist == "log-normal") {
pr <- pnorm(-V, lower.tail = FALSE, log.p = FALSE)
} else if (dist == "weibull") {
pr <- pweibull(exp(-V), shape = 1, lower.tail = FALSE, log.p = FALSE)
}
xlimits <- c(0.96 * minbid, 1.04 * maxbid)
ylimits <- c(0, 1)
df <- data.frame(x = bidseq, y = pr)
p <- ggplot(df, aes(x, y)) + geom_line() + xlim(xlimits) + ylim(ylimits)
invisible(p)
}
ggplot.sbchoice <- function (x, ...) {
minbid <- min(x$bid)
maxbid <- max(x$bid)
bidseq <- seq(minbid, maxbid, by = (maxbid - minbid) / 100)
b <- x$coefficients
npar <- length(b)
Xcov <- colMeans(x$covariates)
Vcov <- sum(Xcov * b[-npar])
V <- Vcov + bidseq * b[npar]
dist <- x$distribution
if(dist == "logistic" | dist == "log-logistic") {
pr <- plogis(-V, lower.tail = FALSE, log.p = FALSE)
} else if (dist == "normal" | dist == "log-normal") {
pr <- pnorm(-V, lower.tail = FALSE, log.p = FALSE)
} else if (dist == "weibull") {
pr <- pweibull(exp(-V), shape = 1, lower.tail = FALSE, log.p = FALSE)
}
xlimits <- c(0.96 * minbid, 1.04 * maxbid)
ylimits <- c(0, 1)
df <- data.frame(x = bidseq, y = pr)
p <- ggplot(df, aes(x, y)) + geom_line() + xlim(xlimits) + ylim(ylimits)
invisible(p)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment