Trying to fix combined function without much luck

This commit is contained in:
agdamsbo 2018-10-03 09:53:51 +02:00
parent f32da76505
commit 5e1ae0b4af
2 changed files with 70 additions and 65 deletions

View File

@ -10,53 +10,53 @@
#' cie_test() #' cie_test()
cie_test<-function(y,v1,string,data,logistic=FALSE,cut=0.1,v2=NULL,v3=NULL){ cie_test<-function(y,v1,string,data,logistic=FALSE,cut=0.1,v2=NULL,v3=NULL){
## Calculating variables, that should be included for a change in estimate analysis. ## Calculating variables, that should be included for a change in estimate analysis.
## v1-3 are possible locked variables, y is the outcome vector. ## v1-3 are possible locked variables, y is the outcome vector.
## String defines variables to test, and is provided as vector of variable names. Use dput(). ## String defines variables to test, and is provided as vector of variable names. Use dput().
## From "Modeling and variable selection in epidemiologic analysis." - S. Greenland, 1989. ## From "Modeling and variable selection in epidemiologic analysis." - S. Greenland, 1989.
require(broom) require(broom)
d<-data d<-data
x<-select(d,one_of(c(string))) x<-select(d,one_of(c(string)))
if(logistic==FALSE){ if(logistic==FALSE){
if (is.factor(y)){stop("Some kind of error message would be nice, but y should not be a factor!")} if (is.factor(y)){stop("Some kind of error message would be nice, but y should not be a factor!")}
if (is.null(v2)&is.null(v3)){ if (is.null(v2)&is.null(v3)){
e<-as.numeric(round(coef(lm(y~v1)),3))[1] e<-as.numeric(round(coef(lm(y~v1)),3))[1]
df<-data.frame(pred="base",b=e) df<-data.frame(pred="base",b=e)
for(i in 1:ncol(x)){ for(i in 1:ncol(x)){
m<-lm(y~v1+x[,i]) m<-lm(y~v1+x[,i])
b<-as.numeric(round(coef(m),3))[1] b<-as.numeric(round(coef(m),3))[1]
v<-x[,i] v<-x[,i]
pred<-paste(names(x)[i]) pred<-paste(names(x)[i])
df<-rbind(df,cbind(pred,b)) df<-rbind(df,cbind(pred,b))
} }
t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop")) t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop"))
df<-cbind(df,t) df<-cbind(df,t)
} }
if (!is.null(v2)&is.null(v3)){ if (!is.null(v2)&is.null(v3)){
e<-as.numeric(round(coef(lm(y~v1+v2)),3))[1] e<-as.numeric(round(coef(lm(y~v1+v2)),3))[1]
df<-data.frame(pred="base",b=e) df<-data.frame(pred="base",b=e)
for(i in 1:ncol(x)){ for(i in 1:ncol(x)){
m<-lm(y~v1+v2+x[,i]) m<-lm(y~v1+v2+x[,i])
b<-as.numeric(round(coef(m),3))[1] b<-as.numeric(round(coef(m),3))[1]
v<-x[,i] v<-x[,i]
pred<-paste(names(x)[i]) pred<-paste(names(x)[i])
@ -64,70 +64,70 @@ df<-cbind(df,t)
df<-rbind(df,cbind(pred,b)) df<-rbind(df,cbind(pred,b))
} }
t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop")) t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop"))
df<-cbind(df,t) df<-cbind(df,t)
} }
if (!is.null(v2)&!is.null(v3)){ if (!is.null(v2)&!is.null(v3)){
e<-as.numeric(round(coef(lm(y~v1+v2+v3)),3))[1] e<-as.numeric(round(coef(lm(y~v1+v2+v3)),3))[1]
df<-data.frame(pred="base",b=e) df<-data.frame(pred="base",b=e)
for(i in 1:ncol(x)){ for(i in 1:ncol(x)){
m<-lm(y~v1+v2+v3+x[,i]) m<-lm(y~v1+v2+v3+x[,i])
b<-as.numeric(round(coef(m),3))[1] b<-as.numeric(round(coef(m),3))[1]
v<-x[,i] v<-x[,i]
pred<-paste(names(x)[i]) pred<-paste(names(x)[i])
df<-rbind(df,cbind(pred,b)) df<-rbind(df,cbind(pred,b))
} }
t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop")) t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop"))
df<-cbind(df,t) df<-cbind(df,t)
}} }}
if(logistic==TRUE){ if(logistic==TRUE){
if (!is.factor(y)){stop("Some kind of error message would be nice, but y should be a factor!")} if (!is.factor(y)){stop("Some kind of error message would be nice, but y should be a factor!")}
if (is.null(v2)&is.null(v3)){ if (is.null(v2)&is.null(v3)){
e<-as.numeric(round(exp(coef(glm(y~v1,family=binomial()))),3))[1] e<-as.numeric(round(exp(coef(glm(y~v1,family=binomial()))),3))[1]
df<-data.frame(pred="base",b=e) df<-data.frame(pred="base",b=e)
for(i in 1:ncol(x)){ for(i in 1:ncol(x)){
m<-glm(y~v1+x[,i],family=binomial()) m<-glm(y~v1+x[,i],family=binomial())
b<-as.numeric(round(exp(coef(m)),3))[1] b<-as.numeric(round(exp(coef(m)),3))[1]
v<-x[,i] v<-x[,i]
pred<-paste(names(x)[i]) pred<-paste(names(x)[i])
df<-rbind(df,cbind(pred,b)) df<-rbind(df,cbind(pred,b))
} }
t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop")) t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop"))
df<-cbind(df,t) df<-cbind(df,t)
} }
if (!is.null(v2)&is.null(v3)){ if (!is.null(v2)&is.null(v3)){
e<-as.numeric(round(exp(coef(glm(y~v1+v2,family=binomial()))),3))[1] e<-as.numeric(round(exp(coef(glm(y~v1+v2,family=binomial()))),3))[1]
df<-data.frame(pred="base",b=e) df<-data.frame(pred="base",b=e)
for(i in 1:ncol(x)){ for(i in 1:ncol(x)){
m<-glm(y~v1+v2+x[,i],family=binomial()) m<-glm(y~v1+v2+x[,i],family=binomial())
b<-as.numeric(round(exp(coef(m)),3))[1] b<-as.numeric(round(exp(coef(m)),3))[1]
v<-x[,i] v<-x[,i]
pred<-paste(names(x)[i]) pred<-paste(names(x)[i])
@ -135,30 +135,30 @@ df<-cbind(df,t)
df<-rbind(df,cbind(pred,b)) df<-rbind(df,cbind(pred,b))
} }
t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop")) t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop"))
df<-cbind(df,t) df<-cbind(df,t)
} }
if (!is.null(v2)&!is.null(v3)){ if (!is.null(v2)&!is.null(v3)){
e<-as.numeric(round(exp(coef(glm(y~v1+v2+v3,family=binomial()))),3))[1] e<-as.numeric(round(exp(coef(glm(y~v1+v2+v3,family=binomial()))),3))[1]
df<-data.frame(pred="base",b=e) df<-data.frame(pred="base",b=e)
for(i in 1:ncol(x)){ for(i in 1:ncol(x)){
m<-glm(y~v1+v2+v3+x[,i],family=binomial()) m<-glm(y~v1+v2+v3+x[,i],family=binomial())
b<-as.numeric(round(exp(coef(m)),3))[1] b<-as.numeric(round(exp(coef(m)),3))[1]
v<-x[,i] v<-x[,i]
pred<-paste(names(x)[i]) pred<-paste(names(x)[i])
df<-rbind(df,cbind(pred,b)) df<-rbind(df,cbind(pred,b))
} }
t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop")) t<-c(NA,ifelse(abs(e-as.numeric(df[-1,2]))>=(e*cut),"include","drop"))
df<-cbind(df,t) df<-cbind(df,t)
}} }}
return(df) return(df)

View File

@ -14,18 +14,23 @@
rep_biv<-function(y,v1,string,data,method="pval",logistic=FALSE,ci=FALSE,cut=0.1,v2=NULL,v3=NULL){ rep_biv<-function(y,v1,string,data,method="pval",logistic=FALSE,ci=FALSE,cut=0.1,v2=NULL,v3=NULL){
require(rep_lm) a<-y
require(rep_glm) b<-v1
require(cie_test) s<-string
dat<-data
me<-method
log<-logistic
CI<-ci
ct<-cut
if (method=="pval"&logistic==FALSE){ if (me=="pval"&log==FALSE){
rep_lm(y=y,v1=v1,string=string,data=data,ci=ci) daDoctoR::rep_lm(y=a,v1=b,string=s,data=dat,ci=CI)
} }
if (method=="pval"&logistic==TRUE){ if (me=="pval"&log==TRUE){
rep_lm(y=y,v1=v1,string=string,data=data,ci=ci) daDoctoR::rep_lm(y=a,v1=b,string=s,data=dat,ci=CI)
} }
if (method=="cie"){ if (method=="cie"){
cie_test(y=y,v1=v1,string=string,data=data,logistic=logistic,cut=cut,v2=v2,v3=v3) daDoctoR::cie_test(y=a,v1=b,string=s,data=dat,logistic=log,cut=ct)
} }
return(df) return(df)
} }