## Fisher’s LSD (Least Significant Different) in R

This tutorial shows how to do the post hoc test using Fisher’s LSD (Least Significant Different) in R. When to use Fisher’s LSD  Typically, it is when you are doing one-way ANOVA with more than 2 groups (e.g., 3 groups). Further, you should note that Fisher’s LSD is the most liberal of all post hoc tests. … Read more

Categories R

## Can you use one-way ANOVA for three groups?

Can you use one-way ANOVA for three groups? Yes, you can use one-way ANOVA for three groups (or, three levels). Actually, one-way ANOVA can be used for 2 groups and more than 2 groups. Example of one-way ANOVA for three groups The following is a hypothetical data example for one-way ANOVA for three groups generated … Read more

Categories R

## Setup Hayes PROCESS in R (4 Steps)

The following shows steps of setup Hayes Mediation PROCESS in R. After setting up the PROCESS in R, we can use Model 4 as a simple example. In the following R code, we first download the data from GitHub. Then, run it using the process(). After running the R code above, you should see the … Read more

Categories R

## Meaning of Hessian Matrix from optim() in R

The inverse of the Hessian matrix from optim() can be used as the asymptotic covariance matrix for estimated parameters. The name “asymptotic” is due to the direct replace the σ2 in the variance of estimated parameters with the estimated one, namely $$\hat{\sigma}^2$$. If the sample size is large enough, t-distribution will be very close … Read more

Categories R

## Maximum Likelihood Estimation for Linear Regression in R

To use maximum likelihood estimation (MLE) in linear regression in R, you can use either optim() or mle() function. The following provides detailed R code examples. Data and Model Suppose we want to test the effect of teaching method (new vs. old) on students’ test scores. The following is the data. In the R code, 0 represents ‘old’ … Read more

Categories R

## How to Export DataFrame to CSV in R

You can use the write.csv() from base R or write_csv() from “readr” package to export a dataframe to CSV in R. write.csv() write.csv(df_1,”csv_file_name.csv”, row.names=FALSE) write_csv() write_csv(df_2,”csv_file_name.csv”) Example 1: Use write.csv() The following is the output of the dataframe df_1. > print(df_1) Letters Numbers 1 A 1 2 B 1 3 C 0 4 D 1 … Read more

Categories R

## Generate Vectors only Contains 0 And 1 in R

You can generate a vector only containing 0 and 1 in R using sample() and rbinom() functions. sample: sample(c(0, 1), size = 10, replace = TRUE) rbinom: rbinom(10, size = 1, prob = 0.5) In both sample() and rbinom(): Example 1 The following is the output. > print(vector_1)  0 0 1 1 1 1 … Read more

Categories R

## 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