Continuously updated collection of little helpers (tm) that facilitates my life in analyzing data (mostly comparative datasets) with R.
devtools::install_github("maksimrudnev/LittleHelpers") to install.
- Multilevel helpers
- Multigroup helpers
- Tools for labelled data and Rstudio viewer
- Pipe helpers
- Values, Schwartz, ESS
Explore multilevel data:
cor_withinprints and plots individual correlations within each group.
cor_betweencomputes means and shows group-level correlation between two variables.
scatter_means_ciComputes means by group and plots on scatterplot against each other (shows country-level correlations).
graph_means_ciPlots means by group.
stacked_barComputes proportions cross-table and plots them in a nice way, returns ggplot object, so any further
+scale_x(), etc. codes can be added.
Recode multilevel data:
aggr_and_mergehelps to create group-level variables from individual-level variables and merge them back to the data.frame on the go.
grand_centerQuick grand-mean centering.
group_centerQuick group-mean centering.
Summarize and visualize multilevel regressions:
good_tableLarge function that creates customizable coefficients tables using multiple lmer models; outputs in Rstudio viewer.
potential_interactionsExploratory. If you have no idea what cross-level interactions to look for. Computes pairwise tests of all the possible interactions in the
lmer()model, or simply shows correlations between random effects and group-level variables.
random_interactionPlots cross-level interactions for
lmer()-fitted models. Customizable. Can automatically choose real moderator values close to mean+-(2)SD.
random_plotPlots random effects from
Compute extra stats for multilevel regressions:
explained_variance.merModComputes psudo-R-square for two-level regressions fitted with
vif_merCompute variance inflation factor for multilevel regressions fitted with
lavaan::lavTestScore(), merging parameter labels with parameters and groups names and adding stars. Useful when you decide with between-group contraints might be relaxed.
mgcfa_diagnosePrint comprehensible output to diagnose problems with MGCFA models.
mi_testSeries of measurement invariance tests, analoigous to
Imagine you need to create a list with means, correlations, and regression results. And you like to do it in one single pipe. In general, it is not possible, and you'll have to start a second pipe, probably doing some redundant computations.
Three little functions that allow for branching pipes. It is against Hadley's idea, as pipes are in principle linear, and in general I agree, but sometimes it would be comfy to ramify pipes away. It overcomes native
%T>% by allowing more than one step after cutting the pipe.
ramifySaves current result into temporary object .buf and identifies a point in the pipe where branching will happen. Argument is an id of a ramification.
branchStarts a new branch from the ramify point. (branch(1) can be omitted, as ramify creates the first branch. Second argument is a family of branches, or parent branch. By default it uses the last parent branch created by the last used ramify.
harvestReturns contents of all the branches as a list.
Example that allows it:
data.frame(a=1:5, b=1/(1+exp(6:10)) ) %>% ramify(1) %>% branch(1) %>% colMeans %>% branch(2) %>% lm(a ~ b, .) %>% broom::tidy(.) %>% branch(3) %>% cor %>% ramify(2) %>% branch(1) %>% round(2) %>% branch(2) %>% psych::fisherz(.) %>% harvest(2) %>% harvest
Save'n'go & Append'n'go
savengo is ridiculously simple but very useful function that saves objects from a middle of your pipe and passes the same object to further elements of the pipe. It allows more efficient debugging and less confusing code, in which you don't have to interrupt your pipe every time you need to save an output.
Its sister function
appendngo appends an intermediary product to an existing list or a vector.
By analogy, one can create whatever storing function they need.
## Example 1 #Saves intermediary result as an object called intermediate.result final.result <- dt %>% dplyr::filter(score<.5) %>% savengo("intermediate.result") %>% dplyr::filter(estimated<0) ## Example 2 #Saves intermediary result as a first element of existing list myExistingList final.result <- dt %>% dplyr::filter(score<.5) %>% appendngo(myExistingList, after=0) %>% dplyr::filter(estimated<0)
Tools for labelled data and Rstudio viewer
Know the labels:
label_bookCreates a codebook for data.frames with labels.
Make use of labels:
cor_tablePrints ready-to-publish correlation tables with significance stars.
crosstabSimple cross-tabulation with labels.
Get rid of labels:
drop_labsDrops labels if you don't need them.
untibbleGet rid of tibble and get clean data.frame.
lab_to_facConverts labelled variables to factors.
Make use of Rstudio viewer:
df_to_viewerPuts any data.frame to RStudio viewer. Also works with models and anything that can be passed through
Values, Schwartz, ESS
valueslist of value labels.
download_essDownload European Social Survey data
schwartz_circleDraw Schwartz circle and more with three simple functions:
ess_valuesComputes 2, 4, or 10 value indices as they are measured in ESS.
reverseRecodes variable in reverse order. Works with labels.
replace_by_tableUseful for recoding when matching tables are alsready specified in a table. Particularly useful for translation.
mean_se_lower_upperSimply mean, SE, upper and lower 95% CI.
verbSimply prints its arguments.
renameRenames variables in data.frame without bullshittery.
theme_mrClean theme for ggplot.