-
Notifications
You must be signed in to change notification settings - Fork 4
/
Lec08_ttest.R
72 lines (61 loc) · 1.72 KB
/
Lec08_ttest.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#################################
# ECON 346 Lab: T-Test example
#################################
#Open data and make inflation rates
data<-read.csv("https://raw.githubusercontent.com/hegerty/ECON346/main/Lab2levelsData.csv",header=TRUE)
data<-as.matrix(data)
inf<-100*diff(log(data[,6:9]))
inf<-as.data.frame(cbind(data[-1,1],inf))
head(inf)
#Note that US inflation data is awfully high in 1974
#What is the t-statstic?
mean(inf[,5])
inf[5,5]-mean(inf[,5])
sd(inf[,5])
#take the ratio
#Remember parentheses!
(inf[5,5]-mean(inf[,5]))/sd(inf[,5])
#Compare to critical value
#N > 30, so close to Z c.v.
qt(p = 0.975,df = 47)
############################################
#Do some tests on US inflation data
#Split sample based on disinflation
US<-inf$USP
#Calculate summary stats
#Examine distribution
mean(US)
sd(US)
length(US)
hist(US,(22-11))
#Plot as a time series
plot(inf[,1],US,type="l",xlab="")
#pre- and post- 1981
#Examine data for drop
#Split into pre and post
#Choose an end point based on data
#Graph and data matrix
#Coded so it only needs to be entered once
inf[,c(1,5)]
end<-11
USpre<-US[1:end]
USpost<-US[(end+1):length(US)]
#Why not add a vertical line?
abline(v=inf[end,1],lty=3,col="dark grey",lwd=2)
#Summary stats
mean(USpre)
mean(USpost)
sd(USpre)
sd(USpost)
length(USpre)
length(USpost)
####
#T-Test
#Two ways: R function and step-by-step using formula
#Note degrees of freedom used (based on formula)
#Same answer both ways
t1<-t.test(USpre,USpost,var.equal = FALSE)
t1
t2<-(mean(USpre)-mean(USpost))/sqrt((var(USpre)/length(USpre))+(var(USpost)/length(USpost)))
t2
#Assignment: Try this for Canadian inflation rates, with the split ocurring after 1990