## T- Test in R

R provide only one function t.test to do all the three T-Test - one sample, two sample(independent) & paired

Lets practice two sample T-Test in R

> ##T-Test practice on R

> #Let us create some data ourself and practice the application of T-Test over that

> #weekend sleep

> weekendsleep<-rnorm(525,9,1)

> weekdaysleep<-rnorm(525,5,1)

> boxplot(weekendsleep, weekdaysleep)

> t.test(weekendsleep, weekdaysleep)

#Result below

Welch Two Sample t-test

data: weekendsleep and weekdaysleep

t = 66.411, df = 1047.9, p-value < 2.2e-16

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

3.901298 4.138859

sample estimates:

mean of x mean of y

9.008714 4.988636

p value is much less than .05 therefore we reject null hypothesis.

.....................................................................................................................................

if we did IQ Test of males & females and found the average to be 100, 98 for 1000 male & female and alpha to be .01 and then check the significance using independent sample T-Test than it will come statistically Significant i.e there is a difference. If the test is conducted on only 25 people it will come statistically insignificant .

So the sample size creates difference

"the difference is statistically significant" i.e a difference exists between two variables.

Let see the difference using R

> d1<-rnorm(1000,98,3) # 1000 samples are taken

> d2<-rnorm(1000,100,3)

> t.test(d1,d2)

Welch Two Sample t-test

data: d1 and d2

t = -14.405, df = 1997.2, p-value < 2.2e-16

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-2.162270 -1.644077

sample estimates:

mean of x mean of y

98.05179 99.95497

* when there are 1000 samples p is less than .05 and thus the difference is statistically significant

> d1<-rnorm(10,98,3) . # only 10 samples are taken

> d2<-rnorm(10,100,3)

> t.test(d1,d2)

Welch Two Sample t-test

data: d1 and d2

t = -1.9128, df = 17.941, p-value = 0.07187

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-4.5722062 0.2148273

sample estimates:

mean of x mean of y

97.59530 99.77399

** p value is greater than .05 so the null hypothesis accepted that there is no difference between IQ Level of two groups

.....................................................................................................................................

Let us go for one sample T-Test

We have the sample wages of 30 people and we want to test whether the average wage of population is larger or equal than 9 dollars

so here mu = 9

Hnot: mu >= 9

H1: mu < 9

>t.test(sample.wages, alternative="less", mu =9)

...................................................................................................................................

Parametric Test are those in which the data is normally distributed and the data is derived on equal interval scale for example T-Test and Correlation Analysis.

In non parametric test there is no such limitation as the data to remain normal or equal interval. Various example of non-parametric test are

Chi- Square test

Fisher Exact Probability Test

Mann- Whitney Test

Wilcoxon Signed-Rank Test

Friedman Test

etc...

Some top youtube videos that explained these concept are as below

https://www.youtube.com/watch?v=PaChte5dL2Q&list=PLNPQb2RADnZbl-dyI5fb40uBD8eFJHImr&index=2

https://www.youtube.com/watch?v=5kPxi4tk6Ak&list=PLNPQb2RADnZbl-dyI5fb40uBD8eFJHImr&index=1

https://www.youtube.com/watch?v=e5JJxBb80CQ&list=PLNPQb2RADnZbl-dyI5fb40uBD8eFJHImr&index=3

Lets practice two sample T-Test in R

> ##T-Test practice on R

> #Let us create some data ourself and practice the application of T-Test over that

> #weekend sleep

> weekendsleep<-rnorm(525,9,1)

> weekdaysleep<-rnorm(525,5,1)

> boxplot(weekendsleep, weekdaysleep)

> t.test(weekendsleep, weekdaysleep)

#Result below

Welch Two Sample t-test

data: weekendsleep and weekdaysleep

t = 66.411, df = 1047.9, p-value < 2.2e-16

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

3.901298 4.138859

sample estimates:

mean of x mean of y

9.008714 4.988636

p value is much less than .05 therefore we reject null hypothesis.

.....................................................................................................................................

__STATISTICALLY SIGNIFICANT__if we did IQ Test of males & females and found the average to be 100, 98 for 1000 male & female and alpha to be .01 and then check the significance using independent sample T-Test than it will come statistically Significant i.e there is a difference. If the test is conducted on only 25 people it will come statistically insignificant .

So the sample size creates difference

"the difference is statistically significant" i.e a difference exists between two variables.

Let see the difference using R

> d1<-rnorm(1000,98,3) # 1000 samples are taken

> d2<-rnorm(1000,100,3)

> t.test(d1,d2)

Welch Two Sample t-test

data: d1 and d2

t = -14.405, df = 1997.2, p-value < 2.2e-16

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-2.162270 -1.644077

sample estimates:

mean of x mean of y

98.05179 99.95497

* when there are 1000 samples p is less than .05 and thus the difference is statistically significant

> d1<-rnorm(10,98,3) . # only 10 samples are taken

> d2<-rnorm(10,100,3)

> t.test(d1,d2)

Welch Two Sample t-test

data: d1 and d2

t = -1.9128, df = 17.941, p-value = 0.07187

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-4.5722062 0.2148273

sample estimates:

mean of x mean of y

97.59530 99.77399

** p value is greater than .05 so the null hypothesis accepted that there is no difference between IQ Level of two groups

.....................................................................................................................................

Let us go for one sample T-Test

We have the sample wages of 30 people and we want to test whether the average wage of population is larger or equal than 9 dollars

so here mu = 9

Hnot: mu >= 9

H1: mu < 9

>t.test(sample.wages, alternative="less", mu =9)

...................................................................................................................................

**Parametric and Non-Parametric Test**Parametric Test are those in which the data is normally distributed and the data is derived on equal interval scale for example T-Test and Correlation Analysis.

In non parametric test there is no such limitation as the data to remain normal or equal interval. Various example of non-parametric test are

Chi- Square test

Fisher Exact Probability Test

Mann- Whitney Test

Wilcoxon Signed-Rank Test

Friedman Test

etc...

Some top youtube videos that explained these concept are as below

https://www.youtube.com/watch?v=PaChte5dL2Q&list=PLNPQb2RADnZbl-dyI5fb40uBD8eFJHImr&index=2

https://www.youtube.com/watch?v=5kPxi4tk6Ak&list=PLNPQb2RADnZbl-dyI5fb40uBD8eFJHImr&index=1

https://www.youtube.com/watch?v=e5JJxBb80CQ&list=PLNPQb2RADnZbl-dyI5fb40uBD8eFJHImr&index=3