diff --git a/NAMESPACE b/NAMESPACE index 4b21f71..32edf62 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,6 @@ # Generated by roxygen2: do not edit by hand export(age_calc) -export(cie_test) export(col_fact) export(col_num) export(cpr_check) @@ -15,3 +14,4 @@ export(rep_biv) export(rep_epi_tests) export(rep_glm) export(rep_lm) +export(rep_reg_cie) diff --git a/R/rep_glm.R b/R/rep_glm.R index 4083dae..f8e3ab7 100644 --- a/R/rep_glm.R +++ b/R/rep_glm.R @@ -3,24 +3,31 @@ #' @description 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(). -#' @param string variables to test. Input as c() of columnnames, use dput(). +#' @param str variables to test. Input as c() of columnnames, use dput(). #' @param ci flag to get results as OR with 95% confidence interval. -#' @param data data frame to pull variables from. +#' @param dta data frame to pull variables from. #' @keywords logistic regression #' @export #' @examples -#' rep_glm() +#' l<-50 +#' y<-factor(rep(c("a","b"),l)) +#' x<-rnorm(length(y), mean=50, sd=10) +#' v1<-factor(rep(c("r","s"),length(y)/2)) +#' v2<-sample(1:100, length(y), replace=FALSE) +#' v3<-as.numeric(1:length(y)) +#' d<-data.frame(y,x,v1,v2,v3) +#' preds<-c("v1","v2","x") +#' rep_glm(meas="y",vars="v3",string=preds,ci=F,data=d) + rep_glm<-function(meas,vars,string,ci=FALSE,data){ - ## x is data.frame of predictors, y is vector of an aoutcome as a factor - ## output is returned as coefficient, or if or=TRUE as OR with 95 % CI. - ## require(broom) d<-data x<-data.frame(d[,c(string)]) v<-data.frame(d[,c(vars)]) + names(v)<-c(vars) y<-d[,c(meas)] dt<-cbind(y,v) m1<-length(coef(glm(y~.,family = binomial(),data = dt))) @@ -57,11 +64,9 @@ rep_glm<-function(meas,vars,string,ci=FALSE,data){ for(i in 1:ncol(x)){ dat<-cbind(dt,x[,i]) - m<-glm(y~.,family = binomial(),data=dat) b<-round(coef(m)[-c(1:m1)],3) - pv<-round(tidy(m)$p.value[-c(1:m1)],3) x1<-x[,i] @@ -89,3 +94,6 @@ rep_glm<-function(meas,vars,string,ci=FALSE,data){ return(r) } + + + diff --git a/R/rep_lm.R b/R/rep_lm.R index c113ee9..8c8b3aa 100644 --- a/R/rep_lm.R +++ b/R/rep_lm.R @@ -9,7 +9,15 @@ #' @keywords linear regression #' @export #' @examples -#' rep_lm() +#' l<-50 +#' y<-factor(rep(c("a","b"),l)) +#' x<-rnorm(length(y), mean=50, sd=10) +#' v1<-factor(rep(c("r","s"),length(y)/2)) +#' v2<-sample(1:100, length(y), replace=FALSE) +#' v3<-as.numeric(1:length(y)) +#' d<-data.frame(y,x,v1,v2,v3) +#' preds<-c("v1","v2","v3") +#' rep_lm(meas="x",vars="y",string=preds,ci=F,data=d) rep_lm<-function(meas,vars,string,ci=FALSE,data){ @@ -18,6 +26,7 @@ rep_lm<-function(meas,vars,string,ci=FALSE,data){ d<-data x<-data.frame(d[,c(string)]) v<-data.frame(d[,c(vars)]) + names(v)<-c(vars) y<-d[,c(meas)] dt<-cbind(y,v) m1<-length(coef(lm(y~.,data = dt))) @@ -26,7 +35,7 @@ rep_lm<-function(meas,vars,string,ci=FALSE,data){ if (ci==TRUE){ - df<-data.frame(matrix(ncol = 3)) + df<-data.frame(matrix(NA,ncol = 3)) names(df)<-c("pred","or_ci","pv") for(i in 1:ncol(x)){ @@ -49,7 +58,7 @@ rep_lm<-function(meas,vars,string,ci=FALSE,data){ if (ci==FALSE){ - df<-data.frame(matrix(ncol = 3)) + df<-data.frame(matrix(NA,ncol = 3)) names(df)<-c("pred","b","pv") for(i in 1:ncol(x)){ @@ -85,3 +94,14 @@ rep_lm<-function(meas,vars,string,ci=FALSE,data){ return(r) } + + +l<-50 +y<-factor(rep(c("a","b"),l)) +x<-rnorm(length(y), mean=50, sd=10) +v1<-factor(rep(c("r","s"),length(y)/2)) +v2<-sample(1:100, length(y), replace=FALSE) +v3<-as.numeric(1:length(y)) +d<-data.frame(y,x,v1,v2,v3) +preds<-c("v1","v2","v3") +rep_lm(meas="x",vars="y",string=preds,ci=F,data=d) diff --git a/R/cie_test.R b/R/rep_reg_cie.R similarity index 95% rename from R/cie_test.R rename to R/rep_reg_cie.R index 160e818..f446277 100644 --- a/R/cie_test.R +++ b/R/rep_reg_cie.R @@ -10,9 +10,9 @@ #' @keywords change-in-estimate #' @export #' @examples -#' cie_test() +#' rep_reg_cie() -cie_test<-function(meas,vars,string,data,logistic=FALSE,cut=0.1){ +rep_reg_cie<-function(meas,vars,string,data,logistic=FALSE,cut=0.1){ require(broom) diff --git a/man/rep_glm.Rd b/man/rep_glm.Rd index aacf28d..4f50ecc 100644 --- a/man/rep_glm.Rd +++ b/man/rep_glm.Rd @@ -11,17 +11,25 @@ rep_glm(meas, vars, string, ci = FALSE, data) \item{vars}{variables in model. Input as c() of columnnames, use dput().} -\item{string}{variables to test. Input as c() of columnnames, use dput().} - \item{ci}{flag to get results as OR with 95% confidence interval.} -\item{data}{data frame to pull variables from.} +\item{str}{variables to test. Input as c() of columnnames, use dput().} + +\item{dta}{data frame to pull variables from.} } \description{ 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. } \examples{ -rep_glm() + l<-50 + y<-factor(rep(c("a","b"),l)) + x<-rnorm(length(y), mean=50, sd=10) + v1<-factor(rep(c("r","s"),length(y)/2)) + v2<-sample(1:100, length(y), replace=FALSE) + v3<-as.numeric(1:length(y)) + d<-data.frame(y,x,v1,v2,v3) + preds<-c("v1","v2","x") + rep_glm(meas="y",vars="v3",string=preds,ci=F,data=d) } \keyword{logistic} \keyword{regression} diff --git a/man/rep_lm.Rd b/man/rep_lm.Rd index 5c6ed66..37f4a55 100644 --- a/man/rep_lm.Rd +++ b/man/rep_lm.Rd @@ -21,7 +21,15 @@ rep_lm(meas, vars, string, ci = FALSE, data) For bivariate analyses, to determine which variables to include in adjusted model. } \examples{ -rep_lm() + l<-50 + y<-factor(rep(c("a","b"),l)) + x<-rnorm(length(y), mean=50, sd=10) + v1<-factor(rep(c("r","s"),length(y)/2)) + v2<-sample(1:100, length(y), replace=FALSE) + v3<-as.numeric(1:length(y)) + d<-data.frame(y,x,v1,v2,v3) + preds<-c("v1","v2","v3") + rep_lm(meas="x",vars="y",string=preds,ci=F,data=d) } \keyword{linear} \keyword{regression} diff --git a/man/cie_test.Rd b/man/rep_reg_cie.Rd similarity index 82% rename from man/cie_test.Rd rename to man/rep_reg_cie.Rd index 27a2cc5..7a1d5d7 100644 --- a/man/cie_test.Rd +++ b/man/rep_reg_cie.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/cie_test.R -\name{cie_test} -\alias{cie_test} +% Please edit documentation in R/rep_reg_cie.R +\name{rep_reg_cie} +\alias{rep_reg_cie} \title{A repeated regression function for change-in-estimate analysis} \usage{ -cie_test(meas, vars, string, data, logistic = FALSE, cut = 0.1) +rep_reg_cie(meas, vars, string, data, logistic = FALSE, cut = 0.1) } \arguments{ \item{meas}{Effect meassure. Input as c() of columnnames, use dput().} @@ -23,6 +23,6 @@ cie_test(meas, vars, string, data, logistic = FALSE, cut = 0.1) For bivariate analyses. From "Modeling and variable selection in epidemiologic analysis." - S. Greenland, 1989. } \examples{ -cie_test() +rep_reg_cie() } \keyword{change-in-estimate}