Sunteți pe pagina 1din 19

Statistical Computing using R

R Statistical Computing Package

S.Sampath
University of Madras

12/7/2019 sampath1959@yahoo.com 9840200275 1


Statistical Computing using R

Data Input
 Input via assignment

 Input using scan statement

Importing tables from Excel Worksheet, saved in tab


delimited form (cricket data)

Importing from csv files

Importing from databases using specialized packages

12/7/2019 sampath1959@yahoo.com 9840200275 2


Statistical Computing using R

Descriptive Statistics

 Arithmetic Mean
 Variance/Standard deviation
Quartiles (Q1,Q2,Q3)
 Skewness/Kurtosis based on moments (moments
package)
Kurtosis (Pearson’s measure)

12/7/2019 sampath1959@yahoo.com 9840200275 3


Statistical Computing using R

Correlation using “cor” function


 Kendall’s correlation
[measure of the extent to which the order of the x’s differs from the
order of the y’s] 2 n sign ( x j  xi )

n(n  1) i j sign ( y j  yi )
 Spearman’s correlation
[for ranked data sets]
 Product moment correlation
[Karl-Pearson]

Syntax:
cor(x, y = NULL, method = c("pearson", "kendall", "spearman"))

12/7/2019 sampath1959@yahoo.com 9840200275 4


Statistical Hypotheses
Null and Alternative Hypotheses
Two types of errors
Level of Significance – p value
Confidence intervals
Confidence levels
Statistical Computing using R

Statistical Test : Rule which helps us to


test the validity of a statistical
hypothesis based on a sample
Parametric tests assume the sample
comes from a known type of population
(like normal)
Nonparametric tests do not make any
such assumption
12/7/2019 sampath1959@yahoo.com 9840200275 6
Statistical Computing using R

Testing for Normality

 QQ plot

 Shapiro test

Kolmogrov-Smirnov Test

12/7/2019 sampath1959@yahoo.com 9840200275 7


Statistical Computing using R

Shapiro test
Shapiro test is a non parametric test meant for
testing the normality of a given set of data.

Example call: shapiro.test(whitney)

Shapiro-Wilk normality test

data: whitney
W = 0.9784, p-value = 0.3986

12/7/2019 sampath1959@yahoo.com 9840200275 8


Statistical Computing using R

Kolmogrov-Smirnov One Sample Test


Kolmogrov-Smirnov one sample test can be used to
whether the sample comes from a specified
distribution. Hence can be used for testing the
goodness of fit as well.
Example calls :
ks.test(da,”pnorm”,0,1)
ks.test(da,”pgamma”,3,2)
ks.test(redwell,"pnorm",0,1,alternative="less")
ks.test(redwell,"pnorm",0,1,alternative="greater")

12/7/2019 sampath1959@yahoo.com 9840200275 9


Statistical Computing using R

Testing for mean using t.test


t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"),
mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95, ...)

 One sample test


 Two sample test
 Two sample paired t-test
 Confidence intervals

Testing whether a given value can be the population mean


Testing whether samples come from populations having
the same mean

12/7/2019 sampath1959@yahoo.com 9840200275 10


Statistical Computing using R

extra group ID
1 0.7 1 1
Sleep Data 2 -1.6 1 2
3 -0.2 1 3
4 -1.2 1 4
5 -0.1 1 5
6 3.4 1 6
7 3.7 1 7
8 0.8 1 8
9 0.0 1 9
10 2.0 1 10
11 1.9 2 1
12 0.8 2 2
13 1.1 2 3
14 0.1 2 4
15 -0.1 2 5
16 4.4 2 6
17 5.5 2 7
18 1.6 2 8
19 4.6 2 9
20 3.4 2 10

12/7/2019 sampath1959@yahoo.com 9840200275 11


Statistical Computing using R

Testing for mean


t.test(sleep$extra[sleep$group==1],sleep$extra[sleep$group==2])

t.test(sleep$extra[sleep$group==1],sleep$extra[sleep$group==2],
paired=T)

t.test(mw$Hgt_of_men,mu=68)

t.test(mw$Hgt_of_men,mw$Hgt_of_women, mu=3)

t.test(mw$Hgt_of_men,mw$Hgt_of_women,conf.level=0.90)

t.test(mw$Hgt_of_men,mw$Hgt_of_women,alternative="grea
ter")
12/7/2019 sampath1959@yahoo.com 9840200275 12
Statistical Computing using R

Testing for variance var.test

var.test(x, y, ratio = 1, alternative = c("two.sided", "less",


"greater"), conf.level = 0.95)

Used for testing the equality of variances

x<-rnorm(15,10,2)
y<-rnorm(16,10,4)
var.test(x,y)
12/7/2019 sampath1959@yahoo.com 9840200275 13
Statistical Computing using R

Testing for correlation

cor.test(x, y, alternative = c("two.sided", "less", "greater"),


method = c("pearson", "kendall", "spearman"),
conf.level = 0.95)

12/7/2019 sampath1959@yahoo.com 9840200275 14


Statistical Computing using R

Assessment of tuna quality. We compare the Hunter L


measure of lightness to the averages of consumer panel
scores (recoded as integer values from 1 to 6 and
averaged over 80 such values) in 9 lots of canned tuna.

x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8)

The alternative hypothesis of interest is that the Hunter L


value is positively associated with the panel score.

cor.test(x, y, method = "kendall", alternative = "greater")

12/7/2019 sampath1959@yahoo.com 9840200275 15


Statistical Computing using R

Chi Square Test

Chi-square test for pre-assigned probabilities


(frequency data):
Eg 1: x <- c(A = 20, B = 15, C = 25)
chisq.test(x)
Eg 2: x <- c(89,37,30,28,2)
p <- c(40,20,20,15,5)
chisq.test(x, p = p, rescale.p = TRUE)
Eg 3: x<-round(10*runif(100,0,1))
table(x)
chisq.test(table(x))

12/7/2019 sampath1959@yahoo.com 9840200275 16


Statistical Computing using R

Chi Square Test for Independence of Attributes


Prepare the two-way table using table function of R
when raw data is available and perform the test.
twt<-read.table(file.choose())
table(twt)
chisq.test(table(twt))
Import the data frame and perform the test
twt<-read.table(file.choose())
chisq.test(table(twt))

12/7/2019 sampath1959@yahoo.com 9840200275 17


Statistical Computing using R

One-way ANOVA

InsectSprays
plot(count ~ spray, data = InsectSprays)
anova(lm(count~spray)

12/7/2019 sampath1959@yahoo.com 9840200275 18


Statistical Computing using R

Two-way ANOVA (With and Without interaction)


rbd<-read.csv(file.choose())
attach(rbd)
names(rbd)
anova(lm(StressReduction~Treatment+Gender))
anova(lm(StressReduction~Treatment*Gender))

Aliter :
a2<aov(write~sex+ses)
summary(a2)
TukeyHSD(a2,”ses”) ## Tukey Honest Significant
Difference
12/7/2019 sampath1959@yahoo.com 9840200275 19

S-ar putea să vă placă și