Quick background: I am an R user whose office is trying to move toward Stata. I have another modeling software that we use that requires any data that is a rate to total to 100% (I know there is an associated drop in data precision, but for the purposes of my project it doesn't matter the loss is negligible). So to specifiy and clarify, I have data where I need each column to total to 100 after rounding to the nearest 10th, and I want to have code that will adjust whatever the largest value in the column so that the column total is 100. I have a user-made function in R that does a very good job of this, but I have no idea how to recreate this function in Stata (or if I even can). I have included the function below. I need to be able to run this on rows or columns.
SmartRound <- function(x, digits = 0) {
up <- 10 ^ digits
x <- x * up
y <- floor(x)
indices <- tail(order(x-y), round(sum(x)) - sum(y))
y[indices] <- y[indices] + 1
y / up
}
I have, so far, exclusively used this function within dplyr code such as:
County1 = CountySim %>%
group_by(`COUNTY`) %>%
summarize(n=n()) %>%
mutate(Rate = (n/sum(n))*100) %>%
adorn_totals("row") %>%
mutate_if(is.numeric, ~SmartRound(., 2)) %>%
mutate (IncRate=round((1095.75/n),5))
and it just seems to work whether it is on columns or rows (I think the group_by statement is controlling that).
If anyone can help me translate this R function to Stata, it would make setting up these tables much quicker, easier, and more reproducible.
Thanks,
Chris
SmartRound <- function(x, digits = 0) {
up <- 10 ^ digits
x <- x * up
y <- floor(x)
indices <- tail(order(x-y), round(sum(x)) - sum(y))
y[indices] <- y[indices] + 1
y / up
}
I have, so far, exclusively used this function within dplyr code such as:
County1 = CountySim %>%
group_by(`COUNTY`) %>%
summarize(n=n()) %>%
mutate(Rate = (n/sum(n))*100) %>%
adorn_totals("row") %>%
mutate_if(is.numeric, ~SmartRound(., 2)) %>%
mutate (IncRate=round((1095.75/n),5))
and it just seems to work whether it is on columns or rows (I think the group_by statement is controlling that).
If anyone can help me translate this R function to Stata, it would make setting up these tables much quicker, easier, and more reproducible.
Thanks,
Chris
Comment