-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_winsorize.R
executable file
·38 lines (30 loc) · 1.53 KB
/
02_winsorize.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
# ===========================
# === Winsorize variables ===
# ===========================
# set working directory
setwd('/Users/philippe/Desktop/projects/emotion')
# detach 'other packages' if there are any
if (!is.null(names(sessionInfo()$otherPkgs))) {
invisible(lapply(paste('package:',names(sessionInfo()$otherPkgs),sep=""),detach,character.only=TRUE,unload=TRUE))
}
# activate R environment
if (exists('.rs.restartR', mode = 'function')) { .rs.restartR() }
source('renv/activate.R')
renv::activate(getwd())
renv::restore(prompt = FALSE)
# attach packages to current R session
library(psych)
# load data
df = read.delim('code/derivatives/main.txt', sep = '\t', header = TRUE)
# create list of variables
vars = c(apply(expand.grid(c('neu', 'neg', 'permit', 'distancing', 'neu_permit', 'neu_distancing', 'neg_permit', 'neg_distancing'),c('ER_arousal', 'ER_valence', 'Corru', 'HP', 'SCR'))[c(2,1)], 1, paste, collapse="_"),
apply(expand.grid(c('valence', 'arousal', 'corru', 'hp', 'scr'),c('ERsucc', 'ERsucc_neg', 'ERsucc_neu'))[c(2,1)], 1, paste, collapse="_"))
# winsorize
winsorized = as.data.frame(lapply(df[,vars], winsor, trim = 0.05))
dfshort.winsor = df
dfshort.winsor[,vars] = winsorized[,vars]
colnames(winsorized) = paste0('Wins_', colnames(winsorized))
dflong.winsor = cbind(df,winsorized)
# save data.frame
write.table(dfshort.winsor, 'code/derivatives/main_winsor.txt', sep ='\t', row.names = FALSE, quote = FALSE)
write.table(dflong.winsor, 'code/derivatives/main_winsor_longlist.txt', sep ='\t', row.names = FALSE, quote = FALSE)