Interaction in Linear Regression

This tutorial focuses on interaction between a categorial variable and a continuous variable in linear regression. Note that, in this tutorial, we limit the the categorical variable to be 2 levels. (For a categrocial variable with 3 levels, please refer to my another tutotrial on interaction and coding in linear regression .) Coding Note In … Read more

Dummy and Contrast Codings in Linear Regression

This tutorial explains the differences between dummy coding and contrast coding in linear regression using R code examples. It is worth pointing out that, this tutorial focuses on the categorical independent variable has 3 levels. Short Note Note that, in R, the default reference group in dummy coding uses the first item in an alphabetical … Read more

Changing Reference Level in Dummy Coding in R

You can change the reference level in dummy coding in R by using the following R code. contr.treatment(total_levels, base = Number_reference_level) Step 1: Prepare Data The following R code generates a sample data. X Y 1 1 -0.56047565 2 2 -0.23017749 3 3 1.55870831 4 1 0.07050839 5 2 0.12928774 6 3 1.71506499 7 1 … Read more

Dummy and Contrast Codings in R

 “Dummy” or “treatment” coding is to create dichotomous variables where each level of the categorical variable is contrasted to a specified reference level. Basic Syntax of Dummy and Contrast Coding 1. Dummy Coding The following is the syntax to do dummy coding in R. contr.treatment( number_of_level_of_X ) 2 3 1 0 0 2 1 0 3 … Read more

How to Plot Multiple t-distribution Bell-shaped Curves in R

This tutorial shows how you can plot multiple t-distribution bell-shaped curves in R. In short, it is a combination of dt(), plot(), and lines(). Example 1 of plotting multiple t-distribution bell-shaped curves The following is the R code to plot two t-distribution bell-shaped curves, with degree freedom of 1 and 5. Example 2 of plotting … Read more

Categories R

Comparisons of t-distribution and Normal distribution

This tutorial compares t-distribution and normal distribution by explaining the similarities and connections between t-distribution and normal distribution. Similarities between t-distribution and normal distribution There are a few similarities between t-distribution and normal distribution. The following figure shows the t-distribution density function curve and the standard normal curve. As we can see, as the sample … Read more

How to Simulate a Dataset for Logistic Regression in R

This tutorial shows the steps of simulating a dataset for logistic regression R. Logistic regression is based on the following link function. In particular, the following are the steps for simulating a dataset for logistic regression in R. Step 1: Generate Xs Suppose that we have 2 Xs in the logistic regression, and the following … Read more

Categories R

How to change column names of Pandas DataFrames

There are 2 methods of changing the column names of Pandas Dataframes. The following shows the basic Python code syntax for changing column names of Pandas Dataframes. Method 1: df.rename(columns={‘old_name_1′:’new_name_1’, ‘old_name_2′:’new_name_2’}, inplace=True) Method 2: df = df.rename({‘old_name_1′:’new_name_1’, ‘old_name_2′:’new_name_2’}, axis=1) Example 1 of changing column names of Pandas DataFrames (Method 1) The following is the first … Read more

Combine Lists into an Array in Python

You can use Numpy column_stack() or row_stack() to combine lists into an array. As Columns: np.column_stack((list1, list2,…)) As Rows: np.row_stack((list1, list2,…)) Example 1 of lists to columns The following combines lists into an array using column_stack(). Thus, lists become columns in the array. [[6 2 4] [2 1 1] [3 3 2] [4 4 4] … Read more

Check if an item in a Python list

This tutorial shows examples of checking if an item is in a Python list. Method 1: item in list_name Method 2: list_name.index(item) Method 3: list_name.count(item) Example for method 1: Check if an item in a list The following code checks if the item of number 6 is in a list. The following is the output, … Read more