-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.R
66 lines (58 loc) · 2.35 KB
/
functions.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# function to return number and proportion
sympProp <- function(symp, df, var = "unge_kjonn_t0", value = NULL){
if (!is.null(value)){
df <- df %>% filter(!!as.symbol(var) == value)
}
n <- df %>% select(!!as.symbol(symp)) %>% filter(!!as.symbol(symp) == 1) %>% nrow() %>% unlist()
prop <- df %>% select(!!as.symbol(symp)) %>% summarise(propS = mean(!!as.symbol(symp), na.rm = T) * 100) %>% select(propS) %>% unlist() %>% round(0)
return(paste0(n, " (", prop, "%)"))
}
# function to return mean and sd
meanSd <- function(variable, df, var = "unge_kjonn_t0", value = NULL){
if (!is.null(value)){
df <- df %>% filter(!!as.symbol(var) == value)
}
res <- c(paste0(df %>%
summarise(mean = mean(!!as.symbol(variable), na.rm = T)) %>%
select(mean) %>%
unlist() %>%
round(1),
" (",
df %>%
summarise(sd = sd(!!as.symbol(variable), na.rm = T)) %>%
select(sd) %>%
unlist() %>%
round(1), ")"))
return(res)
}
# function to return number
returnn <- function(variable, df){
res <- df %>%
filter(!is.na(!!as.symbol(variable))) %>%
nrow(.) %>%
unlist()
return(res)
}
# functions to return regression results
regressionLogistic <- function(outcome, covariates, df, var){
formula <- as.formula(paste0(outcome, "~", paste0(covariates, collapse = "+")))
model <- glm(formula, family = binomial(), data = df) %>%
broom::tidy(exp=T, conf.int=T)
output <- model %>%
filter(term == var) %>%
mutate(newVar = paste0(sprintf("%3.2f", estimate), " (", sprintf("%3.2f", conf.low), "-", sprintf("%3.2f", conf.high), ") ", sprintf("%4.3f", p.value))) %>%
select(newVar) %>%
unlist()
return(output)
}
regressionPoisson <- function(outcome, covariates, df, var){
formula <- as.formula(paste0(outcome, "~", paste0(covariates, collapse = "+")))
model <- glm(formula, family = poisson(link = "log"), data = df) %>%
broom::tidy(exp=T, conf.int=T)
output <- model %>%
filter(term == var) %>%
mutate(newVar = paste0(round(estimate,2), " (", round(conf.low,2), "-", round(conf.high,2), ") ", round(p.value, 3))) %>%
select(newVar) %>%
unlist()
return(output)
}