new function 'print_pred_stratum'. see description

This commit is contained in:
Andreas Gammelgaard Damsbo 2021-06-14 10:40:02 +02:00
parent 23827402f8
commit 46db760722
7 changed files with 77 additions and 1 deletions

View File

@ -1,6 +1,6 @@
Package: daDoctoR Package: daDoctoR
Title: Functions For Health Research Title: Functions For Health Research
Version: 0.21.8 Version: 0.21.9
Year: 2021 Year: 2021
Author: Andreas Gammelgaard Damsbo <agdamsbo@pm.me> Author: Andreas Gammelgaard Damsbo <agdamsbo@pm.me>
Maintainer: Andreas Gammelgaard Damsbo <agdamsbo@pm.me> Maintainer: Andreas Gammelgaard Damsbo <agdamsbo@pm.me>

View File

@ -23,6 +23,7 @@ export(print_diff_byvar)
export(print_log) export(print_log)
export(print_olr) export(print_olr)
export(print_pred) export(print_pred)
export(print_pred_stratum)
export(print_reg_diff_bin) export(print_reg_diff_bin)
export(quantile_cut) export(quantile_cut)
export(redcap_clean_csv) export(redcap_clean_csv)

View File

@ -21,6 +21,7 @@ print_pred<-function(meas,adj,data,dec=2,n.by.adj=FALSE,p.val=FALSE){
## - Include ANOVA in output list, flag to include ## - Include ANOVA in output list, flag to include
require(dplyr) require(dplyr)
require(broom)
d<-data d<-data
m<-d[,c(meas)] m<-d[,c(meas)]

38
R/print_pred_stratum.R Normal file
View File

@ -0,0 +1,38 @@
#' Extension to the print_pred function, for by stratum analysis.
#'
#' Outputs list of results from 'print_pred' for the whole data set and for each stratum defined by 'strat'.
#' Suitable to assist in determining whether a variable is a confounder or effect modifier.
#' Ref: https://open.oregonstate.education/epidemiology/chapter/effect-modification/
#' @param meas binary outcome measure variable, column name in data.frame as a string. Can be numeric or factor. Result is calculated accordingly.
#' @param adj variables to adjust for, as string.
#' @param strat stratum to stratify, variable name as string
#' @param data dataframe of data.
#' @param include.stratum flag to set if stratum variable should be included in first analysis of non-stratified data.
#' @keywords stratum
#' @export
#' @examples
#' data('mtcars')
#' mtcars$vs<-factor(mtcars$vs)
#' mtcars$am<-factor(mtcars$am)
#' print_pred_stratum(meas="mpg",strat="vs",adj=c("disp","wt","am"),data=mtcars,include.stratum=TRUE)
print_pred_stratum<-function(meas,adj,strat,data,dec,include.stratum=TRUE){
require(daDoctoR)
require(dplyr)
if (include.stratum==TRUE){
ls<-list(all=print_pred(meas = meas,adj=c(strat,adj),data=data))
}
if (include.stratum==FALSE) {
ls<-list(all=print_pred(meas = meas,adj=adj,data=data))
}
strt<-data[, c(strat)]
for (i in 1:length(levels(factor(strt)))){
d_str<-data[data[[strat]]==levels(data[[strat]])[i], c(meas,adj)]
ls_str<-list(print_pred(meas = meas,adj=adj,data=d_str))
names(ls_str)<-levels(factor(strt))[i]
ls<-append(ls,ls_str)
}
return(ls)
}

View File

@ -16,3 +16,6 @@ I'm currently working on improving the code to be more universal
- Include sample data to use with special functions - Include sample data to use with special functions
- Include coding examples with all functions - Include coding examples with all functions
## New functions
- Test for confounders and/or effect modifiers
- Test for collinearity

31
man/print_pred_stratum.Rd Normal file
View File

@ -0,0 +1,31 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/print_pred_stratum.R
\name{print_pred_stratum}
\alias{print_pred_stratum}
\title{Extension to the print_pred function, for by stratum analysis.}
\usage{
print_pred_stratum(meas, adj, strat, data, dec, include.stratum = TRUE)
}
\arguments{
\item{meas}{binary outcome measure variable, column name in data.frame as a string. Can be numeric or factor. Result is calculated accordingly.}
\item{adj}{variables to adjust for, as string.}
\item{strat}{stratum to stratify, variable name as string}
\item{data}{dataframe of data.}
\item{include.stratum}{flag to set if stratum variable should be included in first analysis of non-stratified data.}
}
\description{
Outputs list of results from 'print_pred' for the whole data set and for each stratum defined by 'strat'.
Suitable to assist in determining whether a variable is a confounder or effect modifier.
Ref: https://open.oregonstate.education/epidemiology/chapter/effect-modification/
}
\examples{
data('mtcars')
mtcars$vs<-factor(mtcars$vs)
mtcars$am<-factor(mtcars$am)
print_pred_stratum(meas="mpg",strat="vs",adj=c("disp","wt","am"),data=mtcars,include.stratum=TRUE)
}
\keyword{stratum}

View File

@ -19,3 +19,5 @@ library(daDoctoR)
# install.packages("daDoctoR.tar.gz", repos = NULL, type = "source") # install.packages("daDoctoR.tar.gz", repos = NULL, type = "source")
# library(daDoctoR) # library(daDoctoR)