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

Difference between Logit and Probit

This tutorial explains the difference between logit and probit in statistics with formulas and examples. Formula and Example for Logit We can start with the following formula. Thus, \( \beta_0+\beta_1x_1+…+\beta_nx_n \) can be from \( -\infty \) to \(+\infty \), and \( p(y=1) \) will be always within the range of \( (0,1) \). We … Read more

How to Read SPSS Files in R

You can use the read_sav() function from the haven library to read SPSS files in R. The syntax of the function is as follows. read_sav(“file_name.sav”) Step 1: Install haven library The first step is to install the library of “haven” into your local computer. install.packages(‘haven’) Step 2: Library it in R Studio As for other libraries, you need … Read more

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

Probability Density Functions in R (Examples)

The tutorial shows examples of how you can use built-in Probability Density Functions (PDF) in R, including PDF for normal distribution (dnorm), uniform distribution (dunif), and exponential distribution (dexp). Example 1: PDF for Normal Distribution Normal distribution PDF dnorm() in R returns the density of probability at 2. Note that it is standard normal distribution … Read more

rep() Function in R (4 Examples)

rep() is a built-in R function that replicates the values in the provided vector. This tutorial shows how to use rep() function in R with 4 examples. Example 1 The following code is to repeat the number 4 twice. Output: [1] 4 4 Example 2 Output: [1] 1 2 3 1 2 3 1 2 3 … Read more

Categories R

Paired t-test in R (2 Examples)

Method 1 for paired t-test is for a situation group 1 and group 2 are two separate vectors, whereas Method 2 is for two groups of data in the same column. Method 1: t.test(group_1, group_2, paired = TRUE) Method 2: t.test(y~group, data=my_data, paired = TRUE) Data and Hypothesis Suppose we would like to test whether … Read more

Categories R