Mean Comparison Tests: Hypothesis Testing (One Sample and Two Sample)

Here we learn some basics about how to perform Mean Comparison Tests: hypothesis testing for one sample test, two-sample independent test, and dependent sample test. We will also learn how to find the p-values for a certain distribution such as t-distribution, and critical region values. We will also see how to perform one-tailed and two-tailed hypothesis tests.

How to Perform One-Sample t-Test in R

A recent article in The Wall Street Journal reported that the 30-year mortgage rate is now less than 6%. A sample of eight small banks in the Midwest revealed the following 30-year rates (in percent)

4.85.36.54.86.15.86.25.6

At the 0.01 significance level (probability of type-I error), can we conclude that the 30-year mortgage rate for small banks is less than 6%?

Manual Calculations for One-Sample t-Test and Confidence Interval

One sample mean comparison test can be performed manually.

# Manual way
X <- c(4.8, 5.3, 6.5, 4.8, 6.1, 5.8, 6.2, 5.6)
xbar <- mean(X)
s <- sd(X)
mu = 6
n = length(X)
df = n - 1 
tcal = (xbar - mu)/(s/sqrt(n) )
tcal
c(xbar - qt(0.995, df = df) * s/sqrt(n), xbar + qt(0.995, df = df) * s/sqrt(n))
Mean Comparison Tests: One sample Confidence Interval

Critical Values from t-Table

# Critical Value for Left Tail
qt(0.01, df = df, lower.tail = T)
# Critical Value for Right Tail
qt(0.99, df = df, lower.tail = T)
# Critical Vale for Both Tails
qt(0.995, df = df)

Finding p-Values

# p-value (altenative is less)
pt(tcal, df = df)
# p-value (altenative is greater)
1 - pt(tcal, df = df)
# p-value (alternative two tailed or not equal to)
2 * pt(tcal, df = df)

Performing One-Sample Confidence Interval and t-test Using Built-in Function

One can perform one sample mean comparison test using built-in functions available in the R Language.

# Left Tail test
t.test(x = X, mu = 6, alternative = c("less"), conf.level = 0.99)
# Right Tail test
t.test(x = X, mu = 6, alternative = c("greater"), conf.level = 0.99)
# Two Tail test
t.test(x = X, mu = 6, alternative = c("two.sided"), conf.level = 0.99)

How to Perform two-Sample t-Test in R

Consider we have two samples stored in two vectors $X$ and $Y$ as shown in R code. We are interested in the Mean Comparison Test among two groups of people regarding (say) their wages in a certain week.

X = c(70, 82, 78, 70, 74, 82, 90)
Y = c(60, 80, 91, 89, 77, 69, 88, 82)

Manual Calculations for Two-Sample t-Test and Confidence Interval

The manual calculation for two sample t-tests as mean comparison test is as follows.

nx = length(X)
ny = length(Y)
xbar = mean(X)
sx = sd(X)
ybar = mean(Y)
sy = sd(Y)
df = nx + ny - 2
# Pooled Standard Deviation/ Variance 
SP = sqrt( ( (nx-1) * sx^2 + (ny-1) * sy^2) / df )
tcal = (( xbar - ybar ) - 0) / (SP *sqrt(1/nx + 1/ny))
tcal
# Confidence Interval
LL <- (xbar - ybar) - qt(0.975, df)* sqrt((SP^2 *(1/nx + 1/ny) ))
UL <- (xbar - ybar) + qt(0.975, df)* sqrt((SP^2 *(1/nx + 1/ny) ))
c(LL, UL)

Finding p-values

# The p-value at the left-hand side of Critical Region 
pt(tcal, df ) 
# The p-value for two-tailed Critical Region 
2 * pt(tcal, df ) 
# The p-value at the right-hand side of Critical Region 
1 - pt(tcal, df)

Finding Critical Values from t-Table

# Left Tail
qt(0.025, df = df, lower.tail = T)
# Right Tail
qt(0.975, df = df, lower.tail = T)
# Both tails
qt(0.05, df = df)

Performing Two-Sample Confidence Interval and T-test using Built-in Function

One can perform two sample mean comparison test using built-in functions in R Language.

# Left Tail test
t.test(X, Y, alternative = c("less"), var.equal = T)
# Right Tail test
t.test(X, Y, alternative = c("greater"), var.equal = T)
# Two Tail test
t.test(X, Y, alternative = c("two.sided"), var.equal = T)

Note if $X$ and $Y$ variables are from a data frame then perform the two-sample t-test using the formula symbol (~). Let’s first make the data frame from vectors $X$ and $$Y.

data <- data.frame(values = c(X, Y), group = c(rep("A", nx), rep("B", ny)))
t.test(values ~ group, data = data, alternative = "less", var.equal = T)
t.test(values ~ group, data = data, alternative = "greater", var.equal = T)
t.test(values ~ group, data = data, alternative = "two.side", var.equal = T)
Frequently Asked Questions About R
Mean Comparison Test in R

To understand probability distributions functions in R click the link: Probability Distributions in R

MCQs in Statistics

Factors in R (Categorical Data): Learning Made Easy

Factors in R Language are used to represent categorical data in the R language. Factors in R can be ordered or unordered. One can think of a factor as an integer vector where each integer has a label. Factors are specially treated by modeling functions such as lm() and glm().  Factors are the data objects used for categorical data and stored as levels. They can store both string and integer variables. 

Using factors with labels is better than using integers as factors are self-describing; having a variable that has values “Male” and “Female” is better than a variable having values 1 and 2.

Creating a Simple Factor in R

The following example creates a simple factor variable that has two levels.

# Simple factor with two levels
x <- factor(c("yes", "yes", "no", "yes", "no"))
# computes frequency of factors
table(x)
# strips out the class
unclass(x)
Factors in R

The order of the levels can be set using the levels argument to factor(). This can be important in linear modeling because the first level is used as the baseline level.

x <- factor(c("yes","yes","no","yes","no"), levels = c("yes","no"))

Naming Factors in R

Factors can be given names using the label argument. The label argument changes the old values of the variable to a new one. For example,

x <- factor(c("yes", "yes", "no", "yes", "no"), levels = c("yes", "no"), label = c(1,2) )
x <- factor(c("yes","yes","no","yes","no"), levels = c("yes","no"), label = c("Level-1", "level-2"))

x <- factor(c("yes","yes","no","yes","no"), levels = c("yes","no"), label = c("group-1", "group-2"))

Suppose, you have a factor variable with numerical values. You want to compute the mean. The mean vector will result in the average value of the vector, but the mean of the factor variable will result in a warning message. To calculate the mean of the original numeric values of the "f" variable, you have to convert the values using the level argument. For example,

# vector
v <- c(10,20,20,50,10,20,10,50,20)
# vector converted to factor
f <- factor(v)
# mean of the vector
mean(v)

# mean of factor
mean(f)
mean(as.numeric(levels(f)[f]))

Use of cut() Function in R

The the cut() function in R can also be used to convert a numeric variable into a factor. The breaks argument can be used to describe how ranges of numbers will be converted to factor values. If the breaks argument is set to a single number then the resulting factor will be created by dividing the range of the variable into that number of equal-length intervals. However, if a vector of values is given to the breaks argument, the values in the vectors are used to determine the breakpoint. The number of levels of the resultant factor will be one less than the number of values in the vector provided to the breaks argument. For example,

attach(mtcars)
cut(mpg, breaks = 3)
factors <- cut(mpg, breaks = c(10, 18, 25, 30, 35) )
table(factors)
Factors in R using Cut Function

You will notice that the default label for factors produced by the cut() function in R contains the actual range of values that were used to divide the variable into factors.

Learn about Data Frames in R

https://itfeature.com

Introduction: Matrices in R

While dealing with matrices in R, all columns in the matrix must have the same mode (numeric, character, etc.), and the same length. A matrix is a two-dimensional rectangular data set. It can be created using a vector input to the function matrix() in R.

The general syntax of creating matrices in R is:

matrix_name <- matrix(vector, nrow = r, ncol = c,
                         byrow = FALSE, dimnames = list(char_vector_rownames,
                                                        char_vector_colnames)
)

byrow = TRUE indicates that the matrix will be filled by rows.

dimnames provides optional labels for the columns and rows.

Creating Matrices in R

Following the general syntax of the function matrix() in R, let us create a matrix from a vector of the first 20 numbers.

Example 1:

# Generate matrix having 5 rows and 4 columns 
y1 <- matrix (1 : 20, nrow = 5, ncol = 4) ; y1

# Output
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
y2 <- matrix (1 : 20, nrow = 5, ncol = 4, byrow = FALSE); y2

# Output
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
y3 <- matrix (1 : 20, nrow = 5, ncol = 4, byrow = TRUE) ; y3

# Output
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[5,] 17 18 19 20

Example 2:

elements <- c(11, 23, 29, 67)
rownames <- c("R1", "R2")
colnames <- c("C1", "C2")

m1 <- matrix(elements, nrow = 2, ncol = 2, byrow = TRUE, 
      dimnames = list(rownames, colnames)
      )

# Output
   C1 C2
R1 11 23
R2 29 67

Try the above example 2 with the following values set to arguments as below

nrow = 4 and ncol = 1, byrow = FALSE

Note the difference. You may also have some errors related to the number of rows or columns. Therefore, if you change the number of rows or columns then ensure that you have the same number of row names and column names too.

Matrix Operations in R Language

In the R language, there are some operators and functions that can be used to perform computation on one or more matrices. Some basic matrix operations in R are:

Matrix OperationOperator/ Function
Add/ Subtract+, −
Multiply%*%
Transposet( )
Inversesolve ( )
Extract Diagonaldiag( ) It is described at the end too
Determinantdet( )

The following are some examples related to these operators and matrix functions.

m1 <- matrix(c(11, 23, 9, 35), nrow = 2)
m2 <- matrix(c(5, 19, 11, 20), nrow =2)
m3 <- m1 + m2
m4 <- m1 - m2
m5 <- m1 %*% m2
m6 <- m1 / m2
m1t <- t(m1)
m1tminv <- solve(m1t %*% m1)
diag(m1tminv)

# Output
> m1
     [,1] [,2]
[1,]   11    9
[2,]   23   35

> m2
     [,1] [,2]
[1,]    5   11
[2,]   19   20

> m3
     [,1] [,2]
[1,]   16   20
[2,]   42   55

> m4
     [,1] [,2]
[1,]    6   -2
[2,]    4   15

> m5
     [,1] [,2]
[1,]  226  301
[2,]  780  953

> m6
         [,1]      [,2]
[1,] 2.200000 0.8181818
[2,] 1.210526 1.7500000

> m1t
     [,1] [,2]
[1,]   11   23
[2,]    9   35
Introduction: Matrices in R

Some other important functions can be used to perform some required computations on matrices in R. These matrix operations in R are described below for matrix $X$. You can use your matrix.

Consider we have a matrix X with elements.

X <- matrix(1:20, nrow = 4, ncol = 5) 
X
FunctionDescription
rowSums(X)Compute the average value of each column of the Matrix $X$
colSums(X)Compute the average value of each row of the Matrix $X$
rowMeans(X)Compute the average value of each column of the Matrix $X$
colMeans(X)Compute the average value of each column of the Matrix $X$
diag(X)Extract diagonal elements of the Matrix $X$, or
Create a Matrix that has required diagonal elements such as diag(1:5), diag(5),
crossprod(X,X)Compute X‘X. It is a shortcut of t(X)%*%X

Obtaining Regression Coefficients using Matrices in R

Consider we have a dataset that has a response variable and few regressors. There are many ways to create data (or variables), such as one can create a vector for each variable, a data frame for all of the variables, matrices, or can read data stored in a file.

Here we try it using vectors, then bind the vectors where required. We will use matrices to obtain the regression coefficients.

y  <- c(5, 6, 7, 9, 8, 4, 3, 2, 1, 6, 0, 7)
x1 <- c(4, 5, 6, 7, 8, 3, 4, 9, 9, 8, 7, 5)
x2 <- c(10, 22, 23, 10, 11, 14, 15, 16, 17, 12, 11, 17)
x  <- cbind(1, x1, x2)

The cbind( ) function is used to create a matrix x. Note that 1 is also bound to get the intercept term (the model with the intercept term). Let us compute $\beta$’s from OLS using matrix functions and operators.

xt <- t(x)
xtx <- xt %*% x
xtxinv <- solve(xtx)
xty <- xt %*% y
b <- xtxinv %*% xty

The output is

#Output
x
        x1 x2
 [1,] 1  4 10
 [2,] 1  5 22
 [3,] 1  6 23
 [4,] 1  7 10
 [5,] 1  8 11
 [6,] 1  3 14
 [7,] 1  4 15
 [8,] 1  9 16
 [9,] 1  9 17
[10,] 1  8 12
[11,] 1  7 11
[12,] 1  5 17

xt
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
      1    1    1    1    1    1    1    1    1     1     1     1
x1    4    5    6    7    8    3    4    9    9     8     7     5
x2   10   22   23   10   11   14   15   16   17    12    11    17

xtx
         x1   x2
    12   75  178
x1  75  515 1103
x2 178 1103 2854

xtxinv
xty
b
Computing regression coefficient, matrices in R

Data Structure Matrix in R

visit https://gmstat.com