daDoctoR/R/rep_olr.R

87 lines
2.6 KiB
R
Raw Normal View History

#' A repeated ordinal logistic regression function
#'
2018-10-10 13:34:04 +02:00
#' For bivariate analyses. The confint() function is rather slow, causing the whole function to hang when including many predictors and calculating the ORs with CI.
#' @param meas Effect meassure. Input as c() of columnnames, use dput().
#' @param vars variables in model. Input as c() of columnnames, use dput().
2019-11-26 14:11:37 +01:00
#' @param ci flag to get results as OR with 95 percent confidence interval.
#' @param data data.frame to pull variables from.
2019-12-03 13:16:44 +01:00
#' @param ctp cut point for drop/include. Standard 0.1.
2018-10-10 13:34:04 +02:00
#' @keywords olr
#' @export
2019-12-03 13:16:44 +01:00
rep_olr<-function (meas, vars, ci = FALSE, data,ctp=0.1)
{
require(broom)
require(MASS)
2019-12-03 13:16:44 +01:00
d <- dta
x <- data.frame(d[, c(vars)])
names(x) <- c(vars)
y <- d[, c(meas)]
dt <- cbind(y, x)
m1 <- length(coef(polr(y ~ ., data = dt, Hess = TRUE)))
if (!is.factor(y)) {
stop("y should be a factor!")
}
if (ci == TRUE) {
df <- data.frame(matrix(ncol = 3))
names(df) <- c("pred", "or_ci", "pv")
for (i in 1:ncol(x)) {
dat <- data.frame(y = y, x[, i])
names(dat) <- c("y", names(x)[i])
m <- polr(y ~ ., data = dat, Hess = TRUE)
ctable <- coef(summary(m))
2019-12-03 13:16:44 +01:00
conf<-suppressMessages(matrix(exp(confint(m)),ncol=2))
l <- round(conf[,1], 2)
u <- round(conf[,2], 2)
or <- round(exp(coef(m)), 2)
2019-12-03 13:16:44 +01:00
or_ci <- paste0(or, " (", l, " to ", u, ")")
2019-12-03 13:16:44 +01:00
p <- (pnorm(abs(ctable[, "t value"]), lower.tail = FALSE) *
2)[1:length(coef(m))]
pv <- round(p, 3)
2019-12-03 13:16:44 +01:00
x1 <- x[, i]
if (is.factor(x1)) {
pred <- paste(names(x)[i], levels(x1)[-1], sep = "_")
}
else {
pred <- names(x)[i]
}
df <- rbind(df, cbind(pred, or_ci, pv))
}
}
if (ci == FALSE) {
df <- data.frame(matrix(ncol = 3))
names(df) <- c("pred", "b", "pv")
for (i in 1:ncol(x)) {
dat <- data.frame(y = y, x[, i])
names(dat) <- c("y", names(x)[i])
m <- polr(y ~ ., data = dat, Hess = TRUE)
2018-10-09 14:30:27 +02:00
ctable <- coef(summary(m))
2019-12-03 13:16:44 +01:00
b <- round(coef(m), 2)
2018-10-09 14:30:27 +02:00
2019-12-03 13:16:44 +01:00
p <- (pnorm(abs(ctable[, "t value"]), lower.tail = FALSE) *
2)[1:length(coef(m))]
pv <- round(p, 3)
2019-12-03 13:16:44 +01:00
x1 <- x[, i]
if (is.factor(x1)) {
pred <- paste(names(x)[i], levels(x1)[-1], sep = "_")
}
2019-12-03 13:16:44 +01:00
else {
pred <- names(x)[i]
}
df <- rbind(df, cbind(pred, b, pv))
}
}
pa <- as.numeric(df[, c("pv")])
t <- ifelse(pa <= ctp, "include", "drop")
pa <- ifelse(pa < 0.001, "<0.001", pa)
pa <- ifelse(pa <= 0.05 | pa == "<0.001", paste0("*", pa),
ifelse(pa > 0.05 & pa <= 0.1, paste0(".", pa), pa))
r <- data.frame(df[, 1:2], pa, t)[-1, ]
return(r)
}