0% found this document useful (0 votes)
27 views6 pages

R Lab Hypothesis Testing

Uploaded by

farazabdullah3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

R Lab Hypothesis Testing

Uploaded by

farazabdullah3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

R-LAB

1. Z Score and Critical Values

• To get critical values of the z-score in R for one-tailed and two-tailed tests, you can use
the qnorm() function.

• For a one-tailed test with a significance level of α, the critical value is the z-score that
corresponds to a cumulative probability of 1-α in the standard normal distribution.

Example
For example, to get the critical value of the z-score for a one-tailed test with a significance level
of 0.05 (i.e., α=0.05) in R, you can use the following code:

# One-tailed test with significance level of 0.05 (to the right)


critical_value <- qnorm(1-0.05, lower.tail=TRUE)
critical_value
[1] 1.644854
# One-tailed test with significance level of 0.05 (to the left)
critical_value <- qnorm(0.05, lower.tail=TRUE)
critical_value
[1] -1.644854
## Alternatively
critical_value <- qnorm(1-0.05, lower.tail=FALSE)
critical_value
[1] -1.644854
# Two-tailed test with significance level of 0.05
critical_value1 <- qnorm(0.025, lower.tail=TRUE)
critical_value2 <- qnorm(0.025, lower.tail=FALSE)
critical_value1
[1] -1.959964
critical_value2
[1] 1.959964

2. Z-Score for one sample

3. x_bar <- 45 # sample mean


4. mu <- 48 #population mean
5. n <- 50 # Sample size
6. sigma <- 12 # Standard deviation
7. zscore <- ((x_bar - mu)/(sigma/sqrt(n)))
8. zscore
9. [1] -1.767767

3. Computing Z-Score (standardized) for difference in means


Example

sample1 <- c(34,45,30,50,36,45)


sampple2<- c(30,33,40,34,40,54)
# Calculate the z-score standardized for the difference in means
z_score <- (mean(sample1) - mean(sample2)) / sqrt(var(sample1)/length(sampl
e1) + var(sample2)/length(sample2))
z_score
[1] 0.317524

4. P-value

# One-tailed test with z-score of 1.5


p_value <- pnorm(1.5, lower.tail=FALSE)
p_value
[1] 0.0668072
# Two-tailed test with z-score of -2.0
p_value <- 2*pnorm(-2.0, lower.tail=TRUE)
p_value
[1] 0.04550026

5. Critical Values for a t-test

• To get the critical values for a t-test in R, we can use the qt() function.
• The qt() function takes three arguments: p, df, and lower.tail.
• The p argument specifies the probability of the tail, df specifies the degrees of freedom,
and lower.tail specifies whether to calculate the critical value for the lower tail or upper
tail of the t-distribution.

Example
If we want to find the t critical value for a left-tailed, right-tailed and two-tailed tests with a
significance level of .05 and degrees of freedom = 22, we can use the following example code:
#find t critical value (left-tail test)
qt(p=.05, df=22, lower.tail=TRUE)
[1] -1.717144
#find t critical value (right-tail)
qt(p=.05, df=22, lower.tail=FALSE)
[1] 1.717144
#find two-tailed t critical values
qt(p=.05/2, df=22, lower.tail=FALSE)
[1] 2.073873

6. P-value for t score


Example
For example, if we want to find the p-value associated with a t-score of -0.77 and df = 15 in a
left-tailed hypothesis test, we can use the following code:

#find p-value
pt(q=-.77, df=15, lower.tail=TRUE)
[1] 0.2266283
# find two-tailed p-value
p_value <- 2 * pt(q = abs(2.5), df = 10, lower.tail = FALSE)
p_value
[1] 0.03144684

Example
A researcher wants to test whether there is a significant difference in the mean scores of two
groups of students (Group A and Group B) on a math test. The mean score for Group A is 85 with
a standard deviation of 6, while the mean score for Group B is 90 with a standard deviation of 7.
The sample size for both groups is 30. The researcher decides to use a significance level of α =
0.05.
What is the calculated t-value and p-value for this experiment?

Solution

x1_bar = 85
x2_bar = 90
s1 = 6 # sample1 standard deviation
s2 = 7 # sample2 standard deviation
n1 = 30 # size of sample1
n2 = 30 # size of sample2

#Computing t-statistics
t = (x1 - x2) / (s1 /sqrt(n1)) + (s1/sqrt(n2))
t
[1] -3.46891
# critical value for a two-tailed test
qt(p=.05/2, df=58, lower.tail=FALSE)
[1] 2.001717
# P-value
p_value <- 2 * pt(q = abs(-3.46891), df = 58, lower.tail = FALSE)
p_value
[1] 0.0009920902

Example
Dependent t-test

# Create a vector of IDs


id <- 1:20

# Create a vector of weights before


weight_before <- c(60, 65, 70, 72, 68, 74, 80, 76, 62, 68, 75, 79, 72, 70,
67, 63, 65, 71, 73, 69)

# Create a vector of weights after


weight_after <- c(61, 64, 72, 70, 70, 75, 81, 78, 63, 67, 74, 80, 73, 71, 6
8, 64, 66, 72, 75, 71)

# Create a data frame with the three vectors


weight_data <- data.frame(id, weight_before, weight_after)
head(weight_data,5)
id weight_before weight_after
1 1 60 61
2 2 65 64
3 3 70 72
4 4 72 70
5 5 68 70
attach(weight_data)

To calculate the dependent t-test manually, you can follow these steps:

Calculate the differences between the two samples:

weight_data$dif <-weight_before-weight_after
head(weight_data,5)
id weight_before weight_after dif
1 1 60 61 -1
2 2 65 64 1
3 3 70 72 -2
4 4 72 70 2
5 5 68 70 -2

Calculate the mean and standard deviation of the differences:

mean_dif <- mean(weight_data$dif)


sd_dif <- sd(weight_data$dif)

Calculate the standard error of the mean difference:

se_dif <- sd_dif / sqrt(length(weight_data$dif))


se_dif
[1] 0.2575185

Calculate the t-statistic:

t_stat <- mean_dif / se_dif


t_stat
[1] -3.106573

Calculate the degrees of freedom:

dof <- length(weight_data$dif) - 1


dof
[1] 19

Get the critical value (two tailed test)

qt(p=.05/2, df=19, lower.tail=FALSE)


[1] 2.093024

Calculate the p-value using the cumulative distribution function of the t-distribution:

# find two-tailed p-value


p_value <- 2 * pt(q = abs(-3.106573), df = 19, lower.tail = FALSE)
p_value
[1] 0.005809366

You might also like