Skip to content

Commit

Permalink
Apply automatic stylistic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jan 5, 2024
1 parent 77ca86d commit 52c674e
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions R/svyglm.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ svyregress.display <- function(svyglm.obj, decimal = 2) {
xs <- attr(model$terms, "term.labels")
y <- names(model$model)[1]
gaussianT <- ifelse(length(grep("gaussian", model$family)) == 1, T, F)

if (length(xs) == 0) {
stop("No independent variable")
} else if (length(xs) == 1) {
uni <- cbind(data.frame(coefNA(model))[-1, ], data.frame(stats::confint(model))[-1, ])
rn.uni <- lapply(list(uni), rownames)
# uni <- data.frame(summary(survey::svyglm(as.formula(paste(y, " ~ ", xs)), design = design.model, family = model$family))$coefficients)[-1, ]
if (gaussianT) {
summ <- paste(round(uni[, 1], decimal), " (", round(uni[,5], decimal), ",", round(uni[,6], decimal), ")", sep = "")
summ <- paste(round(uni[, 1], decimal), " (", round(uni[, 5], decimal), ",", round(uni[, 6], decimal), ")", sep = "")
uni.res <- matrix(cbind(summ, ifelse(uni[, 4] <= 0.001, "< 0.001", as.character(round(uni[, 4], decimal + 1)))), nrow = nrow(uni))
colnames(uni.res) <- c(paste("Coeff.(", 100 - 100 * 0.05, "%CI)", sep = ""), "P value")
} else {
summ <- paste(round(exp(uni[, 1]), decimal), " (", round(exp(uni[,5]), decimal), ",", round(exp(uni[,6]), decimal), ")", sep = "")
summ <- paste(round(exp(uni[, 1]), decimal), " (", round(exp(uni[, 5]), decimal), ",", round(exp(uni[, 6]), decimal), ")", sep = "")
uni.res <- matrix(cbind(summ, ifelse(uni[, 4] <= 0.001, "< 0.001", as.character(round(uni[, 4], decimal + 1)))), nrow = nrow(uni))
colnames(uni.res) <- c(paste("OR.(", 100 - 100 * 0.05, "%CI)", sep = ""), "P value")
}
Expand All @@ -49,38 +49,38 @@ svyregress.display <- function(svyglm.obj, decimal = 2) {
data.frame(coefNA(stats::update(model, formula(paste(paste(c(". ~ .", xs), collapse = " - "), " + ", v)), design = design.model)))[-1, ], data.frame(stats::confint(stats::update(model, formula(paste(paste(c(". ~ .", xs), collapse = " - "), " + ", v)), design = design.model)))[-1, ]
)
})

# uni <- lapply(xs, function(v){
# summary(survey::svyglm(as.formula(paste(y, " ~ ", v)), design = design.model))$coefficients[-1, ]
# })
rn.uni <- lapply(uni, rownames)
uni <- Reduce(rbind, uni)

if (gaussianT) {
summ <- paste(round(uni[, 1], decimal), " (", round(uni[,5], decimal), ",", round(uni[,6], decimal), ")", sep = "")
summ <- paste(round(uni[, 1], decimal), " (", round(uni[, 5], decimal), ",", round(uni[, 6], decimal), ")", sep = "")
uni.res <- t(rbind(summ, ifelse(uni[, 4] <= 0.001, "< 0.001", as.character(round(uni[, 4], decimal + 1)))))
colnames(uni.res) <- c(paste("crude coeff.(", 100 - 100 * 0.05, "%CI)", sep = ""), "crude P value")
rownames(uni.res) <- rownames(uni)
# mul <- summary(model)$coefficients[-1, ]
mul <- cbind(coefNA(model)[-1, ], stats::confint(model)[-1, ])
mul.summ <- paste(round(mul[, 1], decimal), " (", round(mul[,5], decimal), ",", round(mul[,6], decimal), ")", sep = "")
mul.summ <- paste(round(mul[, 1], decimal), " (", round(mul[, 5], decimal), ",", round(mul[, 6], decimal), ")", sep = "")
mul.res <- t(rbind(mul.summ, ifelse(mul[, 4] <= 0.001, "< 0.001", as.character(round(mul[, 4], decimal + 1)))))
colnames(mul.res) <- c(paste("adj. coeff.(", 100 - 100 * 0.05, "%CI)", sep = ""), "adj. P value")
} else {
summ <- paste(round(exp(uni[, 1]), decimal), " (", round(exp(uni[,5]), decimal), ",", round(exp(uni[,6]), decimal), ")", sep = "")
summ <- paste(round(exp(uni[, 1]), decimal), " (", round(exp(uni[, 5]), decimal), ",", round(exp(uni[, 6]), decimal), ")", sep = "")
uni.res <- t(rbind(summ, ifelse(uni[, 4] <= 0.001, "< 0.001", as.character(round(uni[, 4], decimal + 1)))))
colnames(uni.res) <- c(paste("crude OR.(", 100 - 100 * 0.05, "%CI)", sep = ""), "crude P value")
rownames(uni.res) <- rownames(uni)
# mul <- summary(model)$coefficients[-1, ]
mul <- cbind(coefNA(model)[-1, ], stats::confint(model)[-1, ])
mul.summ <- paste(round(exp(mul[, 1]), decimal), " (", round(exp(mul[,5]), decimal), ",", round(exp(mul[,6]), decimal), ")", sep = "")
mul.summ <- paste(round(exp(mul[, 1]), decimal), " (", round(exp(mul[, 5]), decimal), ",", round(exp(mul[, 6]), decimal), ")", sep = "")
mul.res <- t(rbind(mul.summ, ifelse(mul[, 4] <= 0.001, "< 0.001", as.character(round(mul[, 4], decimal + 1)))))
colnames(mul.res) <- c(paste("adj. OR.(", 100 - 100 * 0.05, "%CI)", sep = ""), "adj. P value")
}
res <- cbind(uni.res[rownames(uni.res) %in% rownames(mul.res), ], mul.res)
rownames(res) <- rownames(mul)
}

fix.all <- res
mdata <- model$model
## rownames
Expand All @@ -93,7 +93,7 @@ svyregress.display <- function(svyglm.obj, decimal = 2) {
fix.all.list[[x]] <<- rbind(rep(NA, ncol(fix.all)), fix.all.list[[x]])
})
fix.all.unlist <- Reduce(rbind, fix.all.list)

# rn.list = lapply(xs, function(x){rownames(fix.all)[grepl(x, rownames(fix.all))]})
rn.list <- lapply(1:length(xs), function(x) {
rownames(fix.all)[rownames(fix.all) %in% rn.uni[[x]]]
Expand All @@ -109,37 +109,37 @@ svyregress.display <- function(svyglm.obj, decimal = 2) {
fix.all.unlist <- t(data.frame(fix.all.unlist))
}
rownames(fix.all.unlist) <- unlist(rn.list)

# pv.colnum = which(colnames(fix.all.unlist) %in% c("P value", "crude P value", "adj. P value"))
# for (i in pv.colnum){
# fix.all.unlist[, i] = ifelse(as.numeric(fix.all.unlist[, i]) < 0.001, "< 0.001", round(as.numeric(fix.all.unlist[, i]), decimal + 1))
# }


outcome.name <- names(model$model)[1]


if (gaussianT) {
first.line <- paste("Linear regression predicting ", outcome.name, sep = "", "- weighted data\n")
last.lines <- paste("No. of observations = ",
length(model$y), "\n", "AIC value = ", round(
model$aic,
decimal + 2
), "\n", "\n",
sep = ""
length(model$y), "\n", "AIC value = ", round(
model$aic,
decimal + 2
), "\n", "\n",
sep = ""
)
} else {
first.line <- paste("Logistic regression predicting ", outcome.name, sep = "", "- weighted data\n")
last.lines <- paste("No. of observations = ", nrow(model$model),
"\n", "\n",
sep = ""
"\n", "\n",
sep = ""
)
}

results <- list(
first.line = first.line, table = fix.all.unlist,
last.lines = last.lines
)
class(results) <- c("display", "list")
return(results)
}
}

0 comments on commit 52c674e

Please sign in to comment.