-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv_metric_polymer2.R
258 lines (184 loc) · 12.9 KB
/
cv_metric_polymer2.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
library(foreach)
library(dplyr)
Data.import=function(Path="~/Dropbox/lammps" , filename = "MSD.colmean.matrix.1.txt", temp=c(320,345,370,395,420,445,470,500,550,600),polymer="PMMA"){
# read in data into a matrix
MSDcol=foreach(i=1:length(temp),.combine = cbind)%do% {
# set correct path for the data file
path=paste(Path,"/", polymer,"/atom",temp[i], sep='')
path
setwd(path)
# read in the data
MSDcol=read.table(file=filename,header=TRUE,sep=',')
library(magrittr)
MSDcol%>%dim
MSDcol=MSDcol[[1]]
return(MSDcol)
}
# add a time variable
time=1:dim(MSDcol)[1]
# change data into data frame
MSDcol=cbind(time,MSDcol)%>%as.data.frame()
}
#====================================================================================================
cv_metric_polymer2=function(
Path="~/Dropbox/lammps"
,polymer="PMMA_big"
,temp=seq(300,600,by=20)
,filename="MSD.g0.colmean.Tmean.1.txt"
,method="bs_splines"
,timesteps=500:4001
,k=10
, target="variance_ratio_einstein"
){
MSD.PS.g0=Data.import(Path=Path, filename = filename, temp=temp,polymer=polymer)
# subset the dataset to contain only data for those timestep
MSD.PS.g0=MSD.PS.g0[timesteps,]%>%select(-time)
################################################################
#
MSD.PS.g0=cbind(MSD.PS.g0,time_steps=timesteps)
# change the time units to ps
# change the length unit to nm.
library(foreach)
colnames(MSD.PS.g0)=c(paste("T",temp,sep=""),"time_steps")
index_list=caret::createFolds(timesteps, k = k, list = TRUE, returnTrain = FALSE)
# this function assumes that the time_steps are at the end of the dataset.
all_temperatures_cv_metrics=foreach(temperature=seq_along(temp),.combine = rbind)%do%{
cv_performance_metrics=foreach(fold=1:k,.combine = rbind)%do%{
index_fold=index_list[[fold]]
test_data=MSD.PS.g0[index_fold,]
train_data=MSD.PS.g0[-index_fold,]
# the order of the term is reflected in the name.
regression_model=switch(method,
mixall={ lm(train_data[,temperature]~I(time_steps)+I(time_steps^(1/2))+I(time_steps^(1/4)),data=train_data[,c(temperature,dim(train_data)[2])] ) }
,rousse={ lm(train_data[,temperature]~I(time_steps^(1/2)),data=train_data[,c(temperature,dim(train_data)[2])] ) }
,reptation={ lm(train_data[,temperature]~I(time_steps^(1/4)),data=train_data[,c(temperature,dim(train_data)[2])] ) }
,einstein={ lm(train_data[,temperature]~I(time_steps),data=train_data[,c(temperature,dim(train_data)[2])] ) }
,rousse_einstein={ lm(train_data[,temperature]~I(time_steps^(1/2))+I(time_steps),data=train_data[,c(temperature,dim(train_data)[2])] ) }
,rousse_reptation={ lm(train_data[,temperature]~I(time_steps^(1/2))+I(time_steps^(1/4)),data=train_data[,c(temperature,dim(train_data)[2])] ) }
,reptation_einstein={ lm(train_data[,temperature]~I(time_steps^(1/4))+I(time_steps),data=train_data[,c(temperature,dim(train_data)[2])] ) }
,hinges={library(earth);earth(train_data[,temperature]~I(time_steps)+I(time_steps^(1/2))+I(time_steps^(1/4)),data=train_data[,c(temperature,dim(train_data)[2])] )}
,ns_splines={library(splines);lm(train_data[,temperature]~ns(I(time_steps^(1/2))+I(time_steps^(1/4))+I(time_steps)),data=train_data[,c(temperature,dim(train_data)[2])])}
,bs_splines={library(splines);lm(train_data[,temperature]~bs(I(time_steps^(1/2))+I(time_steps^(1/4))+I(time_steps)),data=train_data[,c(temperature,dim(train_data)[2])])}
,hinges2={library(earth);earth(train_data[,temperature]~I(time_steps^(1/4))+I(time_steps)+I(time_steps^(1/2)),nk=5,pmethod="exhaustive",data=train_data[,c(temperature,dim(train_data)[2])] )}
,ns_splines2={library(splines);lm(train_data[,temperature]~ns(I(time_steps^(1/2))+I(time_steps^(1/4))),data=train_data[,c(temperature,dim(train_data)[2])])}
,bs_splines2={library(splines);lm(train_data[,temperature]~bs(I(time_steps^(1/2))+I(time_steps^(1/4))),data=train_data[,c(temperature,dim(train_data)[2])])}
)
library(magrittr)
paste("temperature:",temperature,"Fold:",fold,sep="")%>%message
r_squared=mean((predict(regression_model,train_data)-train_data[,temperature])^2)/mean((mean(train_data[,temperature])-train_data[,temperature])^2)
prediction_msd=predict(regression_model,newdata=test_data)
predicted_r_squared=mean((prediction_msd-test_data[,temperature])^2)/mean(( test_data[,temperature]-mean(test_data[,temperature]) )^2)
# all computation to do squared error comparison.
# linear models performance. aka Einstein model
regression_model_linear=lm(train_data[,temperature]~I(time_steps),data=train_data )
prediction_msd_linear=predict(regression_model_linear,newdata=test_data%>%select(time_steps))
variance_ratio_einstein=sum((prediction_msd-test_data[,temperature])^2) /sum((prediction_msd_linear-test_data[,temperature])^2)
modified_rsquares_einstein = 1 - variance_ratio_einstein
# Rousse model performance
regression_model_rousse=lm(train_data[,temperature]~I(time_steps^(1/2)),data=train_data )
prediction_msd_rousse=predict(regression_model_rousse,newdata=test_data%>%select(time_steps))
variance_ratio_rousse=sum((prediction_msd-test_data[,temperature])^2) /sum((prediction_msd_rousse-test_data[,temperature])^2)
modified_rsquares_rousse = 1 - variance_ratio_rousse
# reptation model performance
regression_model_reptation=lm(train_data[,temperature]~I(time_steps^(1/4)),data=train_data )
prediction_msd_reptation=predict(regression_model_reptation,newdata=test_data%>%select(time_steps))
variance_ratio_reptation=sum((prediction_msd-test_data[,temperature])^2) /sum((prediction_msd_reptation-test_data[,temperature])^2)
modified_rsquares_reptation = 1 - variance_ratio_reptation
cv_metrics=c(r_squared,predicted_r_squared
,variance_ratio_einstein,variance_ratio_rousse,variance_ratio_reptation
,modified_rsquares_einstein,modified_rsquares_rousse,modified_rsquares_reptation
,temp[temperature])
}
}
rownames(all_temperatures_cv_metrics)=1:dim(all_temperatures_cv_metrics)[1]
# cross-validation loop
all_temperatures_cv_metrics=all_temperatures_cv_metrics%>%as.data.frame()
colnames(all_temperatures_cv_metrics)=c("r_squared","predicted_r_squared"
,"variance_ratio_einstein","variance_ratio_rousse","variance_ratio_reptation"
,"modified_rsquares_einstein","modified_rsquares_rousse","modified_rsquares_reptation"
,"temperature")
all_temperatures_cv_metrics=all_temperatures_cv_metrics[,c("temperature",target)]
all_temperatures_cv_metrics.melt=reshape2::melt(all_temperatures_cv_metrics,id.vars="temperature")
all_temperatures_cv_metrics.melt$temperature=as.factor(all_temperatures_cv_metrics.melt$temperature)
# return(all_temperatures_cv_metrics.melt)
library(ggplot2)
graph=ggplot(data=all_temperatures_cv_metrics.melt,aes(x=temperature,y=value,colour=variable))+geom_boxplot()+
xlab("Temperature(K)")+
theme(text = element_text(size=10))
return(graph)
}
# cv_metric_polymer2(Path="~/Dropbox/lammps",polymer="PMMA_big", temp=seq(300,600,by=20),filename="MSD.g0.colmean.Tmean.1.txt"
# ,method="bs_splines",timesteps=500:4001,k=10,target=c("r_squared","predicted_r_squared"))
#
# cv_metric_polymer2(Path="~/Dropbox/lammps",polymer="PMMA_big", temp=seq(300,600,by=20),filename="MSD.g0.colmean.Tmean.1.txt"
# ,method="bs_splines",timesteps=500:4001,k=10,target=c("variance_ratio_reptation","variance_ratio_linear"))
#
# cv_metric_polymer2(Path="~/Dropbox/lammps",polymer="PMMA_big", temp=seq(300,600,by=20),filename="MSD.g0.colmean.Tmean.1.txt"
# ,method="ns_splines",timesteps=500:4001,k=10,target=c("r_squared","predicted_r_squared"))
#
# cv_metric_polymer2(Path="~/Dropbox/lammps",polymer="PMMA_big", temp=seq(300,600,by=20),filename="MSD.g0.colmean.Tmean.1.txt"
# ,method="ns_splines",timesteps=500:4001,k=10,target=c("variance_ratio_reptation","variance_ratio_linear"))
#
# cv_metric_polymer2(Path="~/Dropbox/lammps",polymer="PMMA_big", temp=seq(300,600,by=20),filename="MSD.g0.colmean.Tmean.1.txt"
# ,method="hinges",timesteps=500:4001,k=10,target=c("r_squared","predicted_r_squared"))
#
# cv_metric_polymer2(Path="~/Dropbox/lammps",polymer="PMMA_big", temp=seq(300,600,by=20),filename="MSD.g0.colmean.Tmean.1.txt"
# ,method="hinges",timesteps=500:4001,k=10,target=c("variance_ratio_reptation","variance_ratio_linear"))
#
#
# cv_metric_polymer2(Path="~/Dropbox/lammps",polymer="PMMA_big", temp=seq(300,600,by=20),filename="MSD.g0.colmean.Tmean.1.txt"
# ,method="mixall",timesteps=500:4001,k=10,target=c("r_squared","predicted_r_squared"))
#
# cv_metric_polymer2(Path="~/Dropbox/lammps",polymer="PMMA_big", temp=seq(300,600,by=20),filename="MSD.g0.colmean.Tmean.1.txt"
# ,method="mixall",timesteps=500:4001,k=10,target=c("variance_ratio_reptation","variance_ratio_linear"))
four_combined_plots=function(method="mixall"
,timesteps=200:4001
,polymer="PS_20"
,temp=seq(200,500,by=20)
,k=10
,variance_variables=c("variance_ratio_einstein","variance_ratio_rousse","variance_ratio_reptation")
){
pmma_r_squared_g0=cv_metric_polymer2(Path="~/Dropbox/lammps",polymer=polymer, temp=temp,filename="MSD.g0.colmean.Tmean.1.txt"
,method=method,timesteps=timesteps,k=k,target=c("r_squared","predicted_r_squared") )
pmma_variance_ratio_g0=cv_metric_polymer2(Path="~/Dropbox/lammps",polymer=polymer, temp=temp,filename="MSD.g0.colmean.Tmean.1.txt"
,method=method,timesteps=timesteps,k=k,target=variance_variables )
pmma_r_squared_g1=cv_metric_polymer2(Path="~/Dropbox/lammps",polymer=polymer, temp=temp,filename="MSD.g1.colmean.Tmean.1.txt"
,method=method,timesteps=timesteps,k=k,target=c("r_squared","predicted_r_squared") )
pmma_variance_ratio_g1=cv_metric_polymer2(Path="~/Dropbox/lammps",polymer=polymer, temp=temp,filename="MSD.g1.colmean.Tmean.1.txt"
,method=method,timesteps=timesteps,k=k,target=variance_variables )
# library(latex2exp)
r_squared_pmma_g0_all=rbind(ggplotGrob(pmma_r_squared_g0), ggplotGrob(pmma_variance_ratio_g0)
, size = "first")
r_squared_pmma_g1_all=rbind(ggplotGrob(pmma_r_squared_g1), ggplotGrob(pmma_variance_ratio_g1)
, size = "first")
library(gridExtra)
final_combined_plot=grid.arrange(r_squared_pmma_g0_all,r_squared_pmma_g1_all,nrow=1)
return(final_combined_plot)
}
#
# four_combined_plots(method="mixall",timesteps=200:4001 ,polymer="PS_20",temp=seq(200,500,by=20) ,k=5 )
#
#
#
# four_combined_plots(method="hinges",timesteps=200:4001 ,polymer="PS_20",temp=seq(200,500,by=20) ,k=5 )
#
#
# four_combined_plots(method="ns_splines",timesteps=200:4001 ,polymer="PS_20",temp=seq(200,500,by=20) ,k=5 )
#
#
# four_combined_plots(method="bs_splines",timesteps=200:4001 ,polymer="PS_20",temp=seq(200,500,by=20) ,k=5 )
two_combined_plots = function(method="mixall"
,timesteps=200:4001
,polymer="PS_20"
,temp=seq(200,500,by=20)
,k=10
,variance_variables=c("variance_ratio_einstein","variance_ratio_rousse","variance_ratio_reptation")
){
pmma_variance_ratio_g0=cv_metric_polymer2(Path="~/Dropbox/lammps",polymer=polymer, temp=temp,filename="MSD.g0.colmean.Tmean.1.txt"
,method=method,timesteps=timesteps,k=k,target=variance_variables )
pmma_variance_ratio_g1=cv_metric_polymer2(Path="~/Dropbox/lammps",polymer=polymer, temp=temp,filename="MSD.g1.colmean.Tmean.1.txt"
,method=method,timesteps=timesteps,k=k,target=variance_variables )
library(gridExtra)
final_combined_plot=grid.arrange(pmma_variance_ratio_g0,pmma_variance_ratio_g1,nrow=2)
return(final_combined_plot)
}