-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecking_models.R
More file actions
139 lines (99 loc) · 3.56 KB
/
Copy pathchecking_models.R
File metadata and controls
139 lines (99 loc) · 3.56 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Checking different linear models to explain general relationshiop
# between frequency and ratios
#############
### Model ###
#############
fits_data = read.table("inputs/A-A_fits_data_g2.dat", sep = ",");# data to generate model
#fits_data = read.table("inputs/CoCo_fits_data_g2.dat", sep = ",");# data to generate model
#fits_data = read.table("inputs/fits_data.dat", sep = ",");# data to generate model
colnames(fits_data) = c("r", "f");
#mod = lm(r~ 0 + f + I(f^2) + I(f^3), data = fits_data); # first model
#summary(mod);
mod = lm(r~ 0 + f + I(f^3), data = fits_data);# final model
summary(mod);
AIC(mod);BIC(mod);
res = fit$residuals
fit = lm (r~ 0 + f + I(f^2) + I(f^3), data=fits_data, weights=(1/fits_data$f^2))
summary(fit)
plot(fit)
# This model allows r>0
fit = lm(log(r)~ 0 + f + I(f^2) + I(f^3), data = fits_data); # first model
summary(fit)
plot(fit)
fit = lm(r~ 0 + log(f) + I(log(f)^2) + I(log(f)^3), data = fits_data); # first model
summary(fit)
plot(fit)
fit = lm(log(r)~ 0 + log(f) + I(log(f)^2) + I(log(f)^3), data = fits_data); # first model
summary(fit)
fit = lm(log(r)~ 0 + log(f) + I(log(f)^2), data = fits_data); # first model
summary(fit)
plot(fit)
##############################
### Box-Cox Transformation ###
##############################
# https://datascienceplus.com/how-to-detect-heteroscedasticity-and-rectify-it/
library(caret)
bc_mod = caret::BoxCoxTrans(fits_data$r);
fits_data = cbind(fits_data, bc_r = predict(bc_mod, fits_data$r));
bc_mod = lm(bc_r ~ f, data=fits_data);
plot(bc_mod);
summary(bc_mod);
lmMod <- lm(bc_r ~ 0 + f + I(f^3), data=fits_data)
plot(bc_mod);
summary(bc_mod);
#############
### lmvar ###
#############
library(lmvar)
mu1 = "~ f - 1";
mu2 = "~ f + I(f^2) - 1";
mu3 = "~ f + I(f^2) + I(f^3) - 1";
mu4 = "~ f + I(f^3) - 1";
s1 = "~ f - 1";
s2 = "~ f + I(f^2) - 1";
mu = c(mu1, mu2, mu3, mu4);
s = c(s1, s2);
aic = matrix(NA, nrow = length(s), ncol = length(mu));
bic = matrix(NA, nrow = length(s), ncol = length(mu));
count_i = count_j = 0;
for(i in s){
count_i = count_i + 1;
count_j = 0;
for(j in mu){
count_j = count_j + 1;
Xm = model.matrix(eval(parse(text=j)), fits_data);
Xs = model.matrix(eval(parse(text=i)), fits_data);
fit = lmvar(fits_data$r, X_mu = Xm, X_sigma = Xs, intercept_mu = FALSE);
aic[count_i, count_j] = AIC(fit);
bic[count_i, count_j] = BIC(fit);
}
}
aic = as.data.frame(aic)
colnames(aic) = c("mu1", "mu2", "mu3", "mu4");
rownames(aic) = c("s1", "s2");
bic = as.data.frame(bic)
colnames(bic) = c("mu1", "mu2", "mu3", "mu4");
rownames(bic) = c("s1", "s2");
aic; # lower is better
bic; # lower is better
### plot ###
Xm = model.matrix(eval(parse(text=mu3)), fits_data);
Xs = model.matrix(eval(parse(text=s2)), fits_data);
fit = lmvar(fits_data$r, X_mu = Xm, X_sigma = Xs, intercept_mu = FALSE);
plot(fits_data$f, fits_data$r);
pred = predict(fit, X_mu = Xm, X_sigma = Xs, interval = "prediction"); # predictions
lwr = cbind(fits_data$f, pred[,3]); # lower band
lwr = lwr[order(lwr[,1]), ];
lines(lwr[,1], lwr[,2], col = "red", lwd = 2);
upr = cbind(fits_data$f, pred[,4]); # upper band
upr = upr[order(upr[,1]), ];
lines(upr[,1], upr[,2], col = "red", lwd = 2);
med = cbind(fits_data$f, pred[,1]); # middle point
med = med[order(med[,1]), ];
lines(med[,1], med[,2], col = "blue", lwd = 2);
# Model with intercept
Xm = model.matrix(eval(parse(text=mu3)), fits_data);
Xs = model.matrix(eval(parse(text=s2)), fits_data);
fit = lmvar(fits_data$r, X_mu = Xm, X_sigma = Xs, intercept_mu = TRUE);
aic[count_i, count_j]; AIC(fit);
bic[count_i, count_j]; BIC(fit);