This commit is contained in:
agdamsbo 2018-10-04 21:06:22 +02:00
parent cc48f5cac4
commit f807350a89
7 changed files with 68 additions and 24 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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)

View File

@ -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)

View File

@ -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}

View File

@ -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}

View File

@ -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}