-
Notifications
You must be signed in to change notification settings - Fork 10
/
getDescriptiveResultsCoxRegression.R
executable file
·51 lines (40 loc) · 2.28 KB
/
getDescriptiveResultsCoxRegression.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
getDescriptiveResultsCoxRegression <- function(obj, variable, group.var = NULL,
confidence.level = 0.95,
number.of.decimals = 3){
# This function returns the descriptive statistics for One-Way ANOVA.
# Mean, Std.Dev., SE.Mean and the CI of the true mean.
# Args:
# obj: a data.frame or matrix (data)
# variable: a character string indicating the name of response variable.
# group.var: a character string indicating the name of grouping variable.
# confidence.level: a numeric value in the interval [0-1] indicating the level of confidence interval
# for true population median.
# NOTE: Confidence intervals are estimated using the function "wilcox.test(...)".
# Given intervals are asymptotic. Another alternative is to use ci.median(...) function
# from package "asbio". It gives the confidence interval for true median along with the
# true coverage of the interval.
if (!is.factor(obj[ ,group.var]) && !is.null(group.var)){
return("Warning: Only categorical variables are acceptable as a grouping variable.")
}
obj <- obj[complete.cases(obj[ ,c(variable, group.var)]), ]
n <- nrow(obj) ## number of observations in pairs.
group.categories <- levels(obj[ ,group.var]) ## all categories of given group variable.
# Group response variable by group.var
if(!is.null(group.var)){
obj_grp <- eval(substitute(dplyr::group_by(obj, group.var), list(group.var = as.name(group.var))))
}else{
obj_grp <- obj
}
res <- eval(substitute(dplyr::summarise(obj_grp,
n = n(),
Mean = mean(variable),
'Std.Dev' = sd(variable),
'SE.Mean' = sd(variable) / sqrt(n())),
list(variable = as.name(variable))))
res <- eval(substitute(dplyr::mutate(res, Lower = Mean - qnorm((1 - confidence.level)/2, lower.tail = FALSE)*SE.Mean,
Upper = Mean + qnorm((1 - confidence.level)/2, lower.tail = FALSE)*SE.Mean),
list(confidence.level = as.numeric(confidence.level))))
#attr(res, "class") <- "data.frame"
res[ ,-1] <- round(res[ ,-1], number.of.decimals)
return(res)
}