-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate_data.R
101 lines (67 loc) · 2.46 KB
/
generate_data.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
### Temperatures
# SOURCE : http://www7.ncdc.noaa.gov/CDO/CDODivisionalSelect.jsp
# RETRIEVED : May 5th 2015
temp_emp <- scan("temp_clean.txt")
### CO2 concentration
# SOURCE : http://data.giss.nasa.gov/modelforce/ghgases/Fig1A.ext.txt
# RETRIVED : May 5th 2015
CO2_emp <- scan("CO2_clean.txt")
generate_data <- function (
t.fin, # final time period
t.mod, # the true model, 1 for AAC is a myth, 2 for AAC is fction of GHG
temp.emp = temp_emp,
CO2.emp = CO2_emp
){
########
### Read empirical data
########
# ### Temperatures
#
# # SOURCE : http://www7.ncdc.noaa.gov/CDO/CDODivisionalSelect.jsp
# # RETRIEVED : May 5th 2015
#
# temp.emp <- scan("temp_clean.txt")
# Useful variable
n <- length(temp.emp)
### (empirical) Temperatures LAGGED
temp.emp.lag <- append(NA, temp.emp[1:n-1])
# record distribution of residuals around mean of (linearly) detrended temp
temp.detrend.resid <- ( detrend(temp.emp, tt = 'linear', bp = c())
- mean(detrend(temp.emp, tt = 'linear', bp = c())) )
### Emprical data frame
Emp.D <- data.frame(temp.emp, temp.emp.lag, CO2.emp)
##########
## Generate time series from true model based on calibration
##########
# The calibration of the true model based on empirical temperatures is meant to
# ensure that the simulated time series are somewhat realistic
# Similarly, the choice of sample(temp.detrend.resid,1) to generate noise around
# the trend is meant to guarantee that the simulated time series have roughly
# the same kind of variation around the trend as in the empirical temperature time series.
### If the true model is the autoregressive one (no anthropogenic effect)
if (t.mod == 1){
fit <- lm (temp.emp ~ 0 + temp.emp.lag, data = Emp.D[2:n, ])
temp <- 1:t.fin
temp[1] <- temp.emp[1]
for (t in 2:t.fin){
temp[t] <- (coef(fit)[[1]])*temp[t-1] + sample(temp.detrend.resid,1)
}
}
### If the true model is a function of GHG (anthropogenic)
if (t.mod == 2){
fit <- lm (temp.emp ~ 0 + CO2.emp, data = Emp.D[2:n, ])
temp <- 1:t.fin
for (t in 1:t.fin){
temp[t] <- (coef(fit)[[1]])*CO2.emp[t] + sample(temp.detrend.resid,1)
}
}
#### Generate temperature LAGGED
temp.lag <- append(NA, temp[1:n-1])
### Identify GHG with CO2.emp
GHG <- CO2.emp
######
### Generate data frame
######
D <- data.frame(temp,temp.lag,GHG)
D
}