R Code for Serial Mediation

This tutorial shows how you can write R code for serial mediation. Unless you use Hayes’ R code, I am not aware how you can do serial mediation in R. Thus, I am providing this R code here for your convenience. Note that, the current tutorial is for both mediators and the dependent measure Y … Read more

Categories R

Simulate Data for Poisson Regression in R

This tutorial shows how to simulate a dataset for Poisson regression in R. Step 1: Determine the model Suppose that the following is the model with known population parameters, namely known regression coefficients of 0.2 and 0.08. Of course, in reality, the most likely result is that we do not know such parameters and we … Read more

Bootstrapping Mediation Analysis in R from Scratch

This tutorial explains how to write code for boostrapping mediation analysis in R from scratch. Mediation analysis is often used in academic research to understand the underlying mechanism. Often, you also need to report a bootstrapping confidence interval for the indirect effect. Steps of Bootstrapping Mediation Analysis in R The following shows the steps. (1) … Read more

Categories R

Logistic Regression in R

This tutorial is to show how to do logistic regression in R with examples. The following is the key syntax for logistic regression in R. glm(Y~ X1 + X2, data = data_frame_name, family = “binomial”) Steps of logistic regression in R Step 1: Read data and determine model In this tutorial, we are going to … Read more

Categories R

Convert Dataframe Columns to Vectors in R (2 Examples)

This tutorial shows how you can convert dataframe columns to vectors in R with examples. The following shows the two methods to do it. Method 1: vector<-df$column Method 2: vector<-df[ , ‘column’] Examples of Convert Dataframe columns to Vectors in R Example 1 (Method 1) The following is the R code to read a CSV … Read more

Categories R

Two Sample t-test in R (2 Examples)

This tutorial shows how you can two sample t-test in R. Note that, Two sample t-test is also called independent sample t-test or unpaired sample t-test.  Method 1: Vector format t.test(vector_1, vector_2, var.equal = TRUE) Method 2: Data frame format t.test(Y ~ group, data = df_name , var.equal = TRUE) Data and Hypothesis Suppose you … Read more

Categories R

One Sample t-test in R

The following is the core R syntax to do one sample t-test in R. In particular, Method 1 uses built-in R function, whereas method 2 writes the function to test one sample t-test in R from scratch. Method 1: t.test(vector_name, mu = value_to_compare, alternative = “two.sided”) Method 2: (mean(vector_name)-value_to_compare)/(sd(vector_name)/sqrt(number_of_observation)) Data Example for One Sample t-test … Read more

Johnson Neyman in R (2 detailed examples)

This tutorial explains how you can do Johnson Neyman in R. You can use the package of “interactions” in R do Johnson Neyman in R. The following is the key R syntax. library(interactions) model <- lm(Y ~ X * M, data = data_file_name) johnson_neyman(model = model, pred = X, modx = M) The meaning of … Read more

Categories R

lower.tail in qnorm() and pnorm() (4 Examples)

This tutorial explains how to set and understand lower.tail in qnorm() and pnorm(). qnorm(p, mean, sd, lower.tail = TRUE, log.p = FALSE) pnorm(q, mean, sd, lower.tail = TRUE, log.p = FALSE) For both qnorm and pnorm, the meaning of lower.tail is the same: TRUE means p=F(q). That is, cdf is calculated from -∞ to q. … Read more

Categories R

qnorm() in R: Definition and 5 Examples

This tutorial explains how you can use qnorm() in R with examples. qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) p: the probability. mean: The mean of the normal distribution sample data. The default value is 0. sd: The standard deviation. The default value is 1. lower.tail: By default, lower.tail = TRUE. It means … Read more

Categories R