-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible Plot Ideas for Optmatch Objects #207
Comments
Another idea I came up with. Distance is created with Messy code below. One downside is that the match is needed for distance calculations; perhaps revisit our choice to store only a hash of the distance matrix in an library(ggplot2)
plot.optmatch <- function(optm, match, distance_function = mean) {
# Calculate matched distances and apply `distance_function` to them
mtchdists <- matched.distances(optm, match)
mtchdists <- as.data.frame(do.call(rbind,
lapply(mtchdists, distance_function)))
names(mtchdists) <- "dist"
mtchdists$names <- row.names(mtchdists)
# Calculate table of # treatment and # control
txtctl <- tapply(names(optm), optm, function(x) x)
txtctl <- lapply(txtctl, function(x) {
as.numeric(x %in% row.names(match))
})
txtctl <- data.frame(txt = vapply(txtctl, sum, numeric(1)),
ctl = vapply(txtctl, function(x) sum(1-x), numeric(1)))
txtctl$names <- row.names(txtctl)
# Merge to single data set
alldata <- merge(txtctl, mtchdists, by.x = "names")
# Generate X position. 1:1 is at 0, 1:k is at 1, 2, etc, and j:1 is at -1, -2,
# etc
alldata$x <- alldata$ctl - 1
revdir <- alldata$ctl < alldata$txt
alldata$x[revdir] <- -1*(alldata$txt[revdir] - 1)
# Breaks and labels for x axis
brks <- c(-1*rev(seq_len(max(alldata$txt) - 1)),
0,
seq_len(max(alldata$ctl) - 1))
lbls <- c(paste0(rev(seq_len(max(alldata$txt))), ":1"),
paste0("1:", seq_len(max(alldata$ctl)))[-1])
ggplot(alldata, aes(x = x, y = dist)) +
geom_vline(aes(xintercept = 0), size = 2, alpha = .2) +
geom_point() +
scale_x_continuous(breaks = brks,
labels = lbls, name = "Match Sizes") +
scale_y_continuous(name = "Distance in Match")
}
match <- match_on(ct ~ cost, data = nuclearplants)
fm <- fullmatch(match, data = nuclearplants)
plot(fm, match, mean)
plot(fm, match, max) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Y'all,
This is just a prototype of what graphs of optmatch objects could look like (obviously nicer if using ggplot etc..)
The text was updated successfully, but these errors were encountered: