mirror of
https://github.com/agdamsbo/daDoctoR.git
synced 2024-11-21 11:20:23 +01:00
two new functions - major changes coming
This commit is contained in:
parent
e4be92daab
commit
90819eecaa
@ -1,6 +1,6 @@
|
||||
Package: daDoctoR
|
||||
Title: Functions For Health Research
|
||||
Version: 0.21.1
|
||||
Version: 0.21.2
|
||||
Year: 2021
|
||||
Author: Andreas Gammelgaard Damsbo <agdamsbo@pm.me>
|
||||
Maintainer: Andreas Gammelgaard Damsbo <agdamsbo@pm.me>
|
||||
|
@ -18,6 +18,8 @@ export(hwe_geno)
|
||||
export(hwe_sum)
|
||||
export(plot_biv_olr)
|
||||
export(plot_ord_odds)
|
||||
export(print_reg_diff_bin)
|
||||
export(quantile_cut)
|
||||
export(rep_biv)
|
||||
export(rep_epi_tests)
|
||||
export(rep_glm)
|
||||
|
@ -1,8 +1,10 @@
|
||||
#' Calculating age from date of birth
|
||||
#'
|
||||
#' For age calculations.
|
||||
#' @param dob Date of birth.
|
||||
#' @param enddate Date to calculate age at.
|
||||
#' @param dob Date of birth. Data format follows standard POSIX layout. Format is yyyy-mm-dd.
|
||||
#' @param enddate Date to calculate age at. Format is yyyy-mm-dd.
|
||||
#' @param units Default is "years". Can be changed to "days".
|
||||
#' @param precise Default is TRUE. Flag set whether to include calculations of spring years. Only of matter if using units = "days".
|
||||
#' @keywords age
|
||||
#' @export
|
||||
#' @examples
|
||||
@ -12,7 +14,7 @@
|
||||
#' trunc(age_calc(dob,date))
|
||||
|
||||
age_calc<-function (dob, enddate = Sys.Date(), units = "years", precise = TRUE)
|
||||
## Build upon the work of Jason P. Becker, as part of tihe eeptools
|
||||
## Build upon the work of Jason P. Becker, as part of the eeptools
|
||||
{
|
||||
if (!inherits(dob, "Date") | !inherits(enddate, "Date")) {
|
||||
stop("Both dob and enddate must be Date class objects")
|
||||
|
109
R/print_reg_diff_bin.R
Normal file
109
R/print_reg_diff_bin.R
Normal file
@ -0,0 +1,109 @@
|
||||
#' SHOULD BE COMBINED WITH strobe_pred (try transfer the REF) - or keep as a fast/slim version??
|
||||
#'
|
||||
#' Print-friendly regression results for easy export to text editor
|
||||
#'
|
||||
#' Printable table of regression analysis by group for outcome measure. Detects whether to perform logistic or linear regression.
|
||||
#' output is list of
|
||||
#' @param meas outcome measure variable name in data-data.frame as a string. Can be numeric or factor. Result is calculated accordingly.
|
||||
#' @param group groups to compare, as string.
|
||||
#' @param var Default is NULL.
|
||||
#' @param adj variables to adjust for, as string.
|
||||
#' @param data dataframe of data.
|
||||
#' @param dec decimals for results, standard is set to 2. Mean and sd is dec-1.
|
||||
#' @keywords strobe
|
||||
#' @export
|
||||
#' @examples
|
||||
#' data('mtcars')
|
||||
#' mtcars$vs<-factor(mtcars$vs)
|
||||
#' mtcars$am<-factor(mtcars$am)
|
||||
#' print_reg_diff_bin(meas="am",group="vs",adj=c("disp","wt"),data=mtcars)
|
||||
|
||||
print_reg_diff_bin<-function(meas,group,var=NULL,adj,data,dec=2){
|
||||
|
||||
d<-data
|
||||
m<-d[,c(meas)]
|
||||
g<-d[,c(group)]
|
||||
|
||||
ads<-d[,c(adj)]
|
||||
|
||||
dat<-data.frame(m,g,ads)
|
||||
|
||||
df<-data.frame(grp=c(group,as.character(levels(g))))
|
||||
|
||||
if(!is.factor(m)){
|
||||
|
||||
mod<-lm(m~g,data=dat)
|
||||
ci<-confint(mod)
|
||||
co<-round(coef(mod)[-1],dec)
|
||||
lo<-round(ci[-1,1],dec)
|
||||
up<-round(ci[-1,2],dec)
|
||||
|
||||
or_ci<-c("REF",paste0(co," (",lo," to ",up,")"))
|
||||
|
||||
amod<-lm(m~.,data=dat)
|
||||
aci<-confint(amod)
|
||||
aco<-round(coef(amod)[2:length(levels(g))],dec)
|
||||
alo<-round(aci[2:length(levels(g)),1],dec)
|
||||
aup<-round(aci[2:length(levels(g)),2],dec)
|
||||
|
||||
aor_ci<-c("REF",paste0(aco," (",alo," to ",aup,")"))
|
||||
|
||||
nr<-c()
|
||||
|
||||
for (r in 1:length(levels(g))){
|
||||
vr<-levels(dat$g)[r]
|
||||
dr<-dat[dat$g==vr,]
|
||||
n<-as.numeric(nrow(dr[!is.na(dr$m),]))
|
||||
mean<-round(mean(dr$m,na.rm = TRUE),dec-1)
|
||||
sd<-round(sd(dr$m,na.rm = TRUE),dec-1)
|
||||
ms<-paste0(mean," (",sd,")")
|
||||
|
||||
nr<-c(nr,n,ms)
|
||||
}
|
||||
irl<-rbind(matrix(NA,ncol=4),cbind(matrix(nr,ncol=2,byrow = TRUE),cbind(or_ci,aor_ci)))
|
||||
colnames(irl)<-c("N","Mean (SD)","Difference","Adjusted Difference")
|
||||
df<-cbind(df,irl)
|
||||
ls<-list(linear.regression=df)
|
||||
}
|
||||
|
||||
if(is.factor(m)){
|
||||
di<-dat
|
||||
|
||||
mod<-glm(m~g,family=binomial(),data=di)
|
||||
ci<-exp(confint(mod))
|
||||
co<-round(exp(coef(mod))[-1],dec)
|
||||
lo<-round(ci[-1,1],dec)
|
||||
up<-round(ci[-1,2],dec)
|
||||
|
||||
or_ci<-c("REF",paste0(co," (",lo," to ",up,")"))
|
||||
|
||||
amod<-glm(m~.,family=binomial(),data=di)
|
||||
aci<-exp(confint(amod))
|
||||
aco<-round(exp(coef(amod))[2:length(levels(g))],dec)
|
||||
alo<-round(aci[2:length(levels(g)),1],dec)
|
||||
aup<-round(aci[2:length(levels(g)),2],dec)
|
||||
|
||||
aor_ci<-c("REF",paste0(aco," (",alo," to ",aup,")"))
|
||||
|
||||
nr<-c()
|
||||
|
||||
for (r in 1:length(levels(g))){
|
||||
vr<-levels(dat$g)[r]
|
||||
dr<-dat[dat$g==vr,]
|
||||
n<-as.numeric(nrow(dr[!is.na(dr$m),]))
|
||||
nl<-levels(m)[2]
|
||||
out<-nrow(dr[dr$m==nl&!is.na(dr$m),])
|
||||
pro<-round(out/n*100,0)
|
||||
rt<-paste0(out," (",pro,"%)")
|
||||
|
||||
nr<-c(nr,n,rt)
|
||||
}
|
||||
irl<-rbind(matrix(NA,ncol=4),cbind(matrix(nr,ncol=2,byrow = TRUE),cbind(or_ci,aor_ci)))
|
||||
colnames(irl)<-c("N",paste0("N.",nl),"OR","Adjusted OR")
|
||||
df<-cbind(df,irl)
|
||||
ls<-list(logistic.regression=df)
|
||||
}
|
||||
|
||||
ls$adjustments<-names(ads)
|
||||
return(ls)
|
||||
}
|
17
R/quantile_cut.R
Normal file
17
R/quantile_cut.R
Normal file
@ -0,0 +1,17 @@
|
||||
#' Easy function for splitting numeric variable in quantiles
|
||||
#'
|
||||
#' Using base/stats functions cut() and quantile().
|
||||
#' @param x Variable to cut.
|
||||
#' @param groups Number of groups.
|
||||
#' @param na.rm Remove NA's. Default is TRUE.
|
||||
#' @param group.names Names of groups to split to. Default is NULL, giving intervals as names.
|
||||
#' @param ordered.f Set resulting vector as ordered. Default is FALSE.
|
||||
#' @keywords quantile
|
||||
#' @export
|
||||
#' @examples
|
||||
#' aa <- as.numeric(sample(1:1000,2000,replace = TRUE))
|
||||
#' summary(quantile_cut(aa,groups=4)) ## Cuts quartiles
|
||||
|
||||
quantile_cut<-function(x,groups,na.rm=TRUE,group.names=NULL,ordered.f=FALSE){
|
||||
cut(x, quantile(x,probs = seq(0, 1, 1/groups), na.rm = na.rm,names = TRUE, type = 7),include.lowest = TRUE,labels = group.names,ordered_result = ordered.f)
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
#' REWRITE UNDERWAY
|
||||
#'
|
||||
#' Print regression results according to STROBE
|
||||
#'
|
||||
#' Printable table of two dimensional regression analysis of group vs variable for outcome measure. By group. Includes p-value
|
||||
|
@ -1,3 +1,5 @@
|
||||
#' REWRITE UNDERWAY
|
||||
#'
|
||||
#' Print regression results according to STROBE
|
||||
#'
|
||||
#' Printable table of three dimensional regression analysis of group vs var for meas. By var. Includes p-values.
|
||||
@ -9,6 +11,11 @@
|
||||
#' @param dec decimals for results, standard is set to 2. Mean and sd is dec-1.
|
||||
#' @keywords strobe
|
||||
#' @export
|
||||
#' @examples
|
||||
#' data('mtcars')
|
||||
#' mtcars$vs<-factor(mtcars$vs)
|
||||
#' mtcars$am<-factor(mtcars$am)
|
||||
#' strobe_diff_byvar(meas="mpg",var="vs",group = "am",adj=c("disp","wt","hp"),data=mtcars)
|
||||
|
||||
strobe_diff_byvar<-function(meas,var,group,adj,data,dec=2){
|
||||
|
||||
|
@ -1,15 +1,24 @@
|
||||
#' OBSOLETE - USE print_reg_diff_bin
|
||||
#'
|
||||
#' Print regression results according to STROBE
|
||||
#'
|
||||
#' Printable table of regression analysis by group for meas. Detects wether to perform logistic or linear regression.
|
||||
#' output is list of
|
||||
#' @param meas outcome meassure variable name in data-data.frame as a string. Can be numeric or factor. Result is calculated accordingly.
|
||||
#' @param groups groups to compare, as string.
|
||||
#' @param group groups to compare, as string.
|
||||
#' @param var Default is NULL
|
||||
#' @param adj variables to adjust for, as string.
|
||||
#' @param data dataframe of data.
|
||||
#' @param dec decimals for results, standard is set to 2. Mean and sd is dec-1.
|
||||
#' @keywords strobe
|
||||
#' @export
|
||||
#' @examples
|
||||
#' data('mtcars')
|
||||
#' mtcars$vs<-factor(mtcars$vs)
|
||||
#' mtcars$am<-factor(mtcars$am)
|
||||
#' strobe_diff_twodim(meas="am",group="vs",adj=c("disp","wt"),data=mtcars)
|
||||
|
||||
strobe_diff_twodim<-function(meas,group,adj,data,dec=2){
|
||||
strobe_diff_twodim<-function(meas,group,var=NULL,adj,data,dec=2){
|
||||
## meas: sdmt
|
||||
## var: rtreat
|
||||
## group: genotype
|
||||
|
@ -1,21 +1,25 @@
|
||||
#' Regression model of predictors according to STROBE, bi- and multivariate.
|
||||
#' Regression model of predictors according to STROBE, bi- and multivariable.
|
||||
#'
|
||||
#' Printable table of regression model according to STROBE for linear or binary outcome-variables.
|
||||
#' Includes borth bivariate and multivariate in the same table.
|
||||
#' Includes both bivariate and multivariate in the same table.
|
||||
#' Output is a list, with the first item being the main "output" as a dataframe.
|
||||
#' Automatically uses logistic regression model for dichotomous outcome variable and linear regression model for continous outcome variable. Linear regression will give estimated adjusted true mean in list.
|
||||
#' Automatically uses logistic regression model for dichotomous outcome variable and linear regression model for continuous outcome variable. Linear regression will give estimated adjusted true mean in list.
|
||||
#' For logistic regression gives count of outcome variable pr variable level.
|
||||
#' @param meas binary outcome meassure variable, column name in data.frame as a string. Can be numeric or factor. Result is calculated accordingly.
|
||||
#' @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 data dataframe of data.
|
||||
#' @param dec decimals for results, standard is set to 2. Mean and sd is dec-1.
|
||||
#' @param n.by.adj flag to indicate wether to count number of patients in adjusted model or overall for outcome meassure not NA.
|
||||
#' @param n.by.adj flag to indicate whether to count number of patients in adjusted model or overall for outcome measure not NA.
|
||||
#' @param p.val flag to include p-values in table, set to FALSE as standard.
|
||||
#' @keywords logistic
|
||||
#' @export
|
||||
|
||||
strobe_pred<-function(meas,adj,data,dec=2,n.by.adj=FALSE,p.val=FALSE){
|
||||
|
||||
## Wish list:
|
||||
## - SPEED, maybe flags to include/exclude time consuming tasks
|
||||
## - Include ANOVA in output list, flag to include
|
||||
|
||||
require(dplyr)
|
||||
|
||||
d<-data
|
||||
|
@ -7,9 +7,13 @@
|
||||
age_calc(dob, enddate = Sys.Date(), units = "years", precise = TRUE)
|
||||
}
|
||||
\arguments{
|
||||
\item{dob}{Date of birth.}
|
||||
\item{dob}{Date of birth. Data format follows standard POSIX layout. Format is yyyy-mm-dd.}
|
||||
|
||||
\item{enddate}{Date to calculate age at.}
|
||||
\item{enddate}{Date to calculate age at. Format is yyyy-mm-dd.}
|
||||
|
||||
\item{units}{Default is "years". Can be changed to "days".}
|
||||
|
||||
\item{precise}{Default is TRUE. Flag set whether to include calculations of spring years. Only of matter if using units = "days".}
|
||||
}
|
||||
\description{
|
||||
For age calculations.
|
||||
|
35
man/print_reg_diff_bin.Rd
Normal file
35
man/print_reg_diff_bin.Rd
Normal file
@ -0,0 +1,35 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/print_reg_diff_bin.R
|
||||
\name{print_reg_diff_bin}
|
||||
\alias{print_reg_diff_bin}
|
||||
\title{SHOULD BE COMBINED WITH strobe_pred (try transfer the REF) - or keep as a fast/slim version??}
|
||||
\usage{
|
||||
print_reg_diff_bin(meas, group, var = NULL, adj, data, dec = 2)
|
||||
}
|
||||
\arguments{
|
||||
\item{meas}{outcome measure variable name in data-data.frame as a string. Can be numeric or factor. Result is calculated accordingly.}
|
||||
|
||||
\item{group}{groups to compare, as string.}
|
||||
|
||||
\item{var}{Default is NULL.}
|
||||
|
||||
\item{adj}{variables to adjust for, as string.}
|
||||
|
||||
\item{data}{dataframe of data.}
|
||||
|
||||
\item{dec}{decimals for results, standard is set to 2. Mean and sd is dec-1.}
|
||||
}
|
||||
\description{
|
||||
Print-friendly regression results for easy export to text editor
|
||||
}
|
||||
\details{
|
||||
Printable table of regression analysis by group for outcome measure. Detects whether to perform logistic or linear regression.
|
||||
output is list of
|
||||
}
|
||||
\examples{
|
||||
data('mtcars')
|
||||
mtcars$vs<-factor(mtcars$vs)
|
||||
mtcars$am<-factor(mtcars$am)
|
||||
print_reg_diff_bin(meas="am",group="vs",adj=c("disp","wt"),data=mtcars)
|
||||
}
|
||||
\keyword{strobe}
|
27
man/quantile_cut.Rd
Normal file
27
man/quantile_cut.Rd
Normal file
@ -0,0 +1,27 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/quantile_cut.R
|
||||
\name{quantile_cut}
|
||||
\alias{quantile_cut}
|
||||
\title{Easy function for splitting numeric variable in quantiles}
|
||||
\usage{
|
||||
quantile_cut(x, groups, na.rm = TRUE, group.names = NULL, ordered.f = FALSE)
|
||||
}
|
||||
\arguments{
|
||||
\item{x}{Variable to cut.}
|
||||
|
||||
\item{groups}{Number of groups.}
|
||||
|
||||
\item{na.rm}{Remove NA's. Default is TRUE.}
|
||||
|
||||
\item{group.names}{Names of groups to split to. Default is NULL, giving intervals as names.}
|
||||
|
||||
\item{ordered.f}{Set resulting vector as ordered. Default is FALSE.}
|
||||
}
|
||||
\description{
|
||||
Using base/stats functions cut() and quantile().
|
||||
}
|
||||
\examples{
|
||||
aa <- as.numeric(sample(1:1000,2000,replace = TRUE))
|
||||
summary(quantile_cut(aa,groups=4)) ## Cuts quartiles
|
||||
}
|
||||
\keyword{quantile}
|
@ -2,7 +2,7 @@
|
||||
% Please edit documentation in R/strobe_diff_bygroup.R
|
||||
\name{strobe_diff_bygroup}
|
||||
\alias{strobe_diff_bygroup}
|
||||
\title{Print regression results according to STROBE}
|
||||
\title{REWRITE UNDERWAY}
|
||||
\usage{
|
||||
strobe_diff_bygroup(meas, var, group, adj, data, dec = 2)
|
||||
}
|
||||
@ -20,6 +20,9 @@ strobe_diff_bygroup(meas, var, group, adj, data, dec = 2)
|
||||
\item{dec}{decimals for results, standard is set to 2. Mean and sd is dec-1. pval has 3 decimals.}
|
||||
}
|
||||
\description{
|
||||
Print regression results according to STROBE
|
||||
}
|
||||
\details{
|
||||
Printable table of two dimensional regression analysis of group vs variable for outcome measure. By group. Includes p-value
|
||||
Group and variable has to be dichotomous factor.
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
% Please edit documentation in R/strobe_diff_byvar.R
|
||||
\name{strobe_diff_byvar}
|
||||
\alias{strobe_diff_byvar}
|
||||
\title{Print regression results according to STROBE}
|
||||
\title{REWRITE UNDERWAY}
|
||||
\usage{
|
||||
strobe_diff_byvar(meas, var, group, adj, data, dec = 2)
|
||||
}
|
||||
@ -20,6 +20,15 @@ strobe_diff_byvar(meas, var, group, adj, data, dec = 2)
|
||||
\item{dec}{decimals for results, standard is set to 2. Mean and sd is dec-1.}
|
||||
}
|
||||
\description{
|
||||
Print regression results according to STROBE
|
||||
}
|
||||
\details{
|
||||
Printable table of three dimensional regression analysis of group vs var for meas. By var. Includes p-values.
|
||||
}
|
||||
\examples{
|
||||
data('mtcars')
|
||||
mtcars$vs<-factor(mtcars$vs)
|
||||
mtcars$am<-factor(mtcars$am)
|
||||
strobe_diff_byvar(meas="mpg",var="vs",group = "am",adj=c("disp","wt","hp"),data=mtcars)
|
||||
}
|
||||
\keyword{strobe}
|
||||
|
@ -2,22 +2,34 @@
|
||||
% Please edit documentation in R/strobe_diff_twodim.R
|
||||
\name{strobe_diff_twodim}
|
||||
\alias{strobe_diff_twodim}
|
||||
\title{Print regression results according to STROBE}
|
||||
\title{OBSOLETE - USE print_reg_diff_bin}
|
||||
\usage{
|
||||
strobe_diff_twodim(meas, group, adj, data, dec = 2)
|
||||
strobe_diff_twodim(meas, group, var = NULL, adj, data, dec = 2)
|
||||
}
|
||||
\arguments{
|
||||
\item{meas}{outcome meassure variable name in data-data.frame as a string. Can be numeric or factor. Result is calculated accordingly.}
|
||||
|
||||
\item{group}{groups to compare, as string.}
|
||||
|
||||
\item{var}{Default is NULL}
|
||||
|
||||
\item{adj}{variables to adjust for, as string.}
|
||||
|
||||
\item{data}{dataframe of data.}
|
||||
|
||||
\item{dec}{decimals for results, standard is set to 2. Mean and sd is dec-1.}
|
||||
|
||||
\item{groups}{groups to compare, as string.}
|
||||
}
|
||||
\description{
|
||||
Print regression results according to STROBE
|
||||
}
|
||||
\details{
|
||||
Printable table of regression analysis by group for meas. Detects wether to perform logistic or linear regression.
|
||||
output is list of
|
||||
}
|
||||
\examples{
|
||||
data('mtcars')
|
||||
mtcars$vs<-factor(mtcars$vs)
|
||||
mtcars$am<-factor(mtcars$am)
|
||||
strobe_diff_twodim(meas="am",group="vs",adj=c("disp","wt"),data=mtcars)
|
||||
}
|
||||
\keyword{strobe}
|
||||
|
@ -2,12 +2,12 @@
|
||||
% Please edit documentation in R/strobe_pred.R
|
||||
\name{strobe_pred}
|
||||
\alias{strobe_pred}
|
||||
\title{Regression model of predictors according to STROBE, bi- and multivariate.}
|
||||
\title{Regression model of predictors according to STROBE, bi- and multivariable.}
|
||||
\usage{
|
||||
strobe_pred(meas, adj, data, dec = 2, n.by.adj = FALSE, p.val = FALSE)
|
||||
}
|
||||
\arguments{
|
||||
\item{meas}{binary outcome meassure variable, column name in data.frame as a string. Can be numeric or factor. Result is calculated accordingly.}
|
||||
\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.}
|
||||
|
||||
@ -15,15 +15,15 @@ strobe_pred(meas, adj, data, dec = 2, n.by.adj = FALSE, p.val = FALSE)
|
||||
|
||||
\item{dec}{decimals for results, standard is set to 2. Mean and sd is dec-1.}
|
||||
|
||||
\item{n.by.adj}{flag to indicate wether to count number of patients in adjusted model or overall for outcome meassure not NA.}
|
||||
\item{n.by.adj}{flag to indicate whether to count number of patients in adjusted model or overall for outcome measure not NA.}
|
||||
|
||||
\item{p.val}{flag to include p-values in table, set to FALSE as standard.}
|
||||
}
|
||||
\description{
|
||||
Printable table of regression model according to STROBE for linear or binary outcome-variables.
|
||||
Includes borth bivariate and multivariate in the same table.
|
||||
Includes both bivariate and multivariate in the same table.
|
||||
Output is a list, with the first item being the main "output" as a dataframe.
|
||||
Automatically uses logistic regression model for dichotomous outcome variable and linear regression model for continous outcome variable. Linear regression will give estimated adjusted true mean in list.
|
||||
Automatically uses logistic regression model for dichotomous outcome variable and linear regression model for continuous outcome variable. Linear regression will give estimated adjusted true mean in list.
|
||||
For logistic regression gives count of outcome variable pr variable level.
|
||||
}
|
||||
\keyword{logistic}
|
||||
|
Loading…
Reference in New Issue
Block a user