Home

From loop to purrr

The first instinct for many R users (especially those coming from other programming languages) is to use run of the mill for loop: This works fine. It loops through country_1 to country_8, checks if each column exists, and converts the country names into COW codes. Buuut there’s another way to do this. Enter purrr and…

How to use the assign() function in R

We can use the assign function to create new variables. Most often I want to assign variables that I create to the Global Environment. assign particularly useful in loops, simulations, and scenarios involving conditional variable naming or creation. The basic syntax of the assign function is assign(x, value, pos = -1, envir = as.environment(pos), inherits…

How to run decision tree analysis with xgboost in R (Tidymodels Series PART 3)

Packages we will need: In this blog post, we are going to run boosted decision trees with xgboost in tidymodels. Boosted decision trees are a type of ensemble learning technique. Ensemble learning methods combine the predictions from multiple models to create a final prediction that is often more accurate than any single model’s prediction. The…

How to run linear regression analysis with tidymodels in R for temporal prediction (Tidymodels Series PART 2)

Packages we will need: We will look at Varieties of Democracy dataset We will create two datasets: one for all years EXCEPT 2020 and one for only 2020 First we build the model. We will look at whether level of public sector theft can predict the judicial corruption levels. The model will have three parts…

How to run regressions with the tidymodels package in R: PART 1

The tidymodels framework in R is a collection of packages for modeling. Within tidymodels, the parsnip package is primarily responsible for specifying models in a way that is independent of the underlying modeling engines. The set_engine() function in parsnip allows users to specify which computational engine to use for modeling, enabling the same model specification…

How to use the mget() function in R

The mget() fuction is a multiple get() function We use mget() to retrieve multiple objects by their names I have found this helpful when I want to perform operations on many df (with similar names) without having to type out each name. For example, I can create four data.frames. They all have similar name patterns.…