-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain_script_production_9.R
2486 lines (1875 loc) · 62.3 KB
/
main_script_production_9.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#################################################################################
###
### Make sure I start with a clean slate.
### Set the directory to the latest working directory.
###
###
### Do not run the rm command often.
###
rm(list=ls(all=TRUE))
setwd("C:/Users/proud/Documents/RESEARCH/PROJECT - Superconductivity/Productions/Production_9")
#################################################################################
###
### This part describes the clean up process for the data.
### The superconducting material list was obtained from:
### http://supercon.nims.go.jp/index_en.html
### on July 24th, 2017, 11:15 AM.
###
### Once logged in, I clicked on "OXIDE & METALLIC".
### The site took me to http://supercon.nims.go.jp/supercon/material_menu.
### There, I clicked on the "search" to get *all* the data.
### I took a screen shot of the site. The screen shot file is in "menu.pdf".
###
### 31639 rows of data are indicated.
###
### I clicked on "Data Download" to download the data.
### The file is "material.csv".
###
### I made a copy of "material.csv" into "material_copy.csv".
### "material_copy.csv" is the file used for all the clean up.
###
###
### The following are done manually:
### (1) Removed columns ma1 to mj2.
### (2) Sorted the data by Tc from highest to lowest.
### (3) The critical temp for the following "num" variables were shifted by ONE column to the
### right. I fixed these manually: 31020, 31021, 31022, 31023, 31024, 31025,
### 153150, 153149, 42170, 42171, 30716, 30717, 30718, 30719,
### 150001, 150002, 150003, 150004, 150005, 150006, 150007,
### 30712, 30713, 30714, 30715.
### (4) The following are removed since the cricial temp seem wrong:
### num = 111620, La0.23Th0.77Pb3
### num = 9632, Pb2C1Ag2O6
### num = 140, Er1Ba2Cu3O7-X
### (5) All rows with Tc = 0 or missing are removed.
### (6) Columns nums, mo1, mo2, oz, str3, tcn, tcfig, refno are removed.
### (7) The following changes are made manually:
### Y2C2Br0.5!1.5 is removed.
### Y1Ba2Cu3O6050 is removed. I suspect 6050 is a mistake.
### Nd185Ce0.15Cu1O4 is removed. There was a Nd1.85Ce0.15Cu1O4 already in the data.
### Hg1234O10 is removed. 1234 for Hg???? This doesnot seem correct.
### Bi1.6Pb0.4Sr2Cu3Ca2O1013 is changed to Bi1.6Pb0.4Sr2Cu3Ca2O10.13
### Y1Ba2Cu285Ni0.15O7 is changed to Y1Ba2Cu2.85Ni0.15O7
###
### (9) The following are removed because they have a zero as a coefficient:
### "Bi1.7Pb0.3Sr2Ca1Cu2O0" (Zero of Oxygen?)
### "La1.85Nd0Ca1.15Cu2O5.99"
### "Bi0Mo0.33Cu2.67Sr2Y1O7.41"
### "Y0.5Yb0.5Ba2Sr0Cu3O7"
### (10) These are removed because they don't make sense:
### Yo975Yb0.025Ba2Cu3O
### Yo975Yb0.025Ba2Cu3O
### Yo975Yb0.025Ba2Cu3O
### (10) Change all oxygen designations such as O8-z to O8, O5+X TO O5, etc.
### Important: I had to do this in the correct sequence; for example find and replace
### Oz+8 before Oz, etc.
### I did this manually! Took a lot of time. I initially approached it by
### string manipulations but trying to take care of the exceptions was taking too much time.
### (11) The column heading changed to "material" and critical_temp
###
###
### This is the fantastic R package that reads and breaks up formulas:
###
install.packages("CHNOSZ")
library(CHNOSZ)
###
### Now upload the data and make sure you get no error messages.
###
dat = read.csv("material_copy.csv", stringsAsFactors = FALSE, header = TRUE)
###
### The following takes about 9 seconds:
###
### Run to see if you see any problems: errors, warning, etc.
### Also gives you an idea about how long looping over 25000 rows
### and using the "makeup" function take.
###
system.time({
for (i in 1:nrow(dat))
{
makeup(dat$material[i])
}
})
###
### I got no error or warning messages.
###
###
### Just look at a snippet of the data:
###
head(dat)
summary(dat)
dim(dat)
###
### No missing values for critical temperatures.
### There are 24861 rows.
###
###
### Let's see how many rows were left out from the original data:
###
tmp = read.csv("material.csv", na.strings = "",
stringsAsFactors = FALSE,
header = TRUE)
dim(tmp)
###
### 31639 - 24861 = 6778 are left out.
###
###
### Clean up:
###
ls()
rm(tmp)
rm(i)
###
### Only the following elements will be considered.
### All the atomic numbers are less than 86.
###
elements = c("H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg",
"Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr",
"Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr",
"Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In",
"Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm",
"Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re",
"Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn")
###
### In this next section, I'll only pick up the materials with atomic number <= 86.
###
###
### The reject object holds the index value of the materials that are rejected.
###
### The following chunk takes about 9 seconds:
###
reject = rep(0, nrow(dat))
system.time({
for (i in 1:nrow(dat))
{
tmp = makeup(dat$material[i])
if (!all(names(tmp) %in% elements)) reject[i] = 1
}
})
###
### How many are rejected?
###
length(which(reject == 1))
###
### 973 are rejected.
###
###
### Pick up the remaining and save data into
### an object called kept_dat.
###
kept_dat = dat[ which(reject == 0), ]
dim(kept_dat)
dim(dat)
###
### Now I have 23888 = 24861 - 973 rows.
###
###
### Save the rejected material for later analysis.
###
rejected_dat = dat[ which(reject == 1), ]
###
### Again, some clean up.
###
ls()
rm(i)
rm(tmp)
###
### The following chunk will take about 265 seconds or about 5 minutes.
###
### Matrix (and later converted to data frame) m will be used later to
### gather summary statistics from the data.
### It could also be used for feature extraction.
###
row_names_kept_dat = row.names(kept_dat)
m = matrix(0, nrow(kept_dat), length(elements) )
colnames(m) = elements
system.time({
for ( i in 1:nrow(kept_dat) )
{
tmp_values = makeup(kept_dat$material[i])
tmp_location_of_matches = match( names(tmp_values), colnames(m) )
m[i, tmp_location_of_matches ] = tmp_values
}
})
###
### Add the temperature back:
###
m = as.data.frame(m)
m$critical_temp = kept_dat$critical_temp
###
### Add the row names back:
###
row.names(m) = row_names_kept_dat
###
### Get only unique rows.
###
dim(m)
unique_m = unique(m)
dim(unique_m)
###
### 23888 - 21263 = 2625 rows omitted.
###
###
### Now add the chemical formulas back to m:
###
m$material = kept_dat$material
###
### Now add the chemical formula's back to unique_m
###
unique_m$material = m$material[row.names(m) %in% row.names(unique_m)]
dim(m)
dim(unique_m)
###
### m: You have 23888 by 2
### unique_m: You have 21263 by 2
###
### 23888 - 21263 = 2625 duplicates
###
###
### I did a final check here:
###
### Here v just records the largest number of atoms
### for an element in a material.
### The intent is to catch any thing suspecious.
###
v = numeric(nrow(unique_m))
for (i in 1:nrow(unique_m))
{
v[i] = max(makeup(unique_m$material[i]))
}
unique_m$material[which(v > 90)]
###
### Most of these look legit but some don't look right. For example, "B105" doesn't look legit.
### They are mostly alloys.
### I will leave these as is.
###
dim(unique_m)
###
### I am down to 21263 material.
### I lost 31639 - 21263 = 10376 rows.
### I have picked up 67% of the data.
###
###
### Remove junk:
###
ls()
rm(i)
rm("row_names_kept_dat")
rm("tmp_location_of_matches")
rm("tmp_values")
rm(v)
ls()
gc()
###
### Next, I upload the data from elements.
### The subfolder has "Production_1" because this data never changed.
###
element_data = read.csv("C:/Users/proud/Documents/RESEARCH/PROJECT - Superconductivity/Productions/Production_1/data_csv.csv",header = T)
###
### Assign row names:
###
rownames(element_data) = element_data$Name
###
### Remove some useless stuff or stuff that can be highly
### correlated with other material.
### This was a judgement call and comes from initial experimentation
### with the element superconductivity.
###
element_data$Name = NULL
element_data$Color = NULL
element_data$ElectricalType = NULL
element_data$QuantumNumbers = NULL
element_data$RefractiveIndex = NULL
element_data$VanDerWaalsRadius = NULL
element_data$VickersHardness = NULL
element_data$BulkModulus = NULL
element_data$NeutronCrossSection = NULL
element_data$NeutronMassAbsorption = NULL
###
### This is what I have at this point:
###
### (1) dat: Data file that has the material, and the critical temperature.
### This is the cleaned data but has *duplicates* and materials with
### elements which could have atomic number over 86.
### (2) rejected_dat : This is the data which had elements with atomic number over 86.
### (8) reject: Index of rejected material in "dat" object.
### (3) kept_dat: This is the dat but with reject_data removed. It has duplicates.
### (2) element_data: This is the original file that has all the elements
### and their properties. This does have a lot of missing values.
### (3) elements: This is just a list of chemical symbols of elements used for the
### analysis.
### (6) m: This is a large data table that in addition to the material names
### and critical temp, it also has 86 columns corresponding to 86 possible elements
### and whether the element listed in the column is in material or not.
### It has duplicates.
### (7) unique_m: This is m with the duplicates removed. It has no duplicates.
###
###
### Save the data for future analysis:
###
save.image("processed_data_production_9.RData")
###
### The file size is around 1.215 MB.
###
###
### This is used for checking. These should be deleted from
### the local directory.
###
write.csv(dat, "dat.csv")
write.csv(m, "m.csv")
write.csv(unique_m, "unique_m.csv")
######################################################################################
######################################################################################
######################################################################################
###
### In this part, I will just visually inspect the data and make some summaries.
###
###
### First let's look at the data I kept.
###
###
### Set up first:
###
library(CHNOSZ)
setwd("C:/Users/proud/Documents/RESEARCH/PROJECT - Superconductivity/Productions/Production_9")
load("processed_data_production_9.RData")
###
### Let's count the number of elements:
###
###
### This function just counts:
###
count_it = function(x)
{
sum(x > 0)
}
###
### This is the table that has the proportions.
### Note we just count columns 1:86. 87 & 88 have
### critical temp and formula name.
###
p_table = apply(unique_m[,1:86], 2, count_it)/nrow(unique_m)
names(p_table) = colnames(unique_m[,1:86])
non_zero_p_table = p_table[which(p_table > 0)]
non_zero_p_table = sort(non_zero_p_table, decreasing = TRUE)
###
### Now plot it:
###
off_set = 0.015
default_mar = c(5, 4, 4, 2) + 0.1 # c(bottom, left, top, right)
### mar c(bottom, left, top, right) = default is c(5, 4, 4, 2) + 0.1
par(mar = c(2, 5, 2, 1) + 0.1)
x = 1:length(non_zero_p_table)
plot( x = x, y = non_zero_p_table, xaxt = "n", xlab = "",
ylab = "Element Proportion", pch = 19,
cex.lab = 1.25, ylim = c(0,max(non_zero_p_table) + off_set * 1.05), cex = 0.65)
abline(h = seq(from = 0, to = 0.6, by = 0.05), lwd = 0.5, col = "grey")
text(x, non_zero_p_table+off_set, names(non_zero_p_table), cex = 0.65)
###
### The plot above is saved in "element_proportions.pdf".
### I did have to resize the plot.
###
###
### I'll also create a top 20 proportion too:
###
non_zero_p_table_top_20 = non_zero_p_table[1:20]
x = 1:length(non_zero_p_table_top_20)
plot( x = x, y = non_zero_p_table_top_20, xaxt = "n", xlab = "",
ylab = "Element Proportion", pch = 19,
cex.lab = 1.25, ylim = c(0,max(non_zero_p_table_top_20) + off_set * 1.05), cex = 0.65)
abline(h = seq(from = 0, to = 0.6, by = 0.05), lwd = 0.5, col = "grey")
text(x, non_zero_p_table_top_20 + off_set* 1.1, names(non_zero_p_table_top_20))
###
### The plot above is saved in "element_proportions_top_20.pdf".
### I did have to resize the plot.
###
###
### Let's save a csv as well
###
write.csv(non_zero_p_table, "non_zero_p_table.csv")
###
### Now collect summary data per element.
### ncol(m) - 2 : This is the number of elements
### 1:86 : Leave out 87 which the formula and 88 which is the temp
### 7 : 7 parameters used.
###
summary_matrix = matrix(NA, ncol(unique_m) - 2, 7)
rownames(summary_matrix) = colnames(unique_m)[1:86]
colnames(summary_matrix) = c("Min", "Q1", "Med", "Q3", "Max", "Mean", "SD")
for ( j in 1:nrow(summary_matrix) )
{
tmp = unique_m[ which(unique_m[,j] > 0), "critical_temp"]
summary_matrix[j,1] = min(tmp)
summary_matrix[j,2] = quantile(tmp, prob = 0.25, names = F)
summary_matrix[j,3] = median(tmp)
summary_matrix[j,4] = quantile(tmp, prob = 0.75, names = F)
summary_matrix[j,5] = max(tmp)
summary_matrix[j,6] = mean(tmp)
summary_matrix[j,7] = sd(tmp)
}
###
### Order by mean & Now plot the means:
###
summary_matrix = summary_matrix[ order(summary_matrix[,6], decreasing = TRUE) , ]
summary_matrix = as.data.frame(summary_matrix)
###
### The last 9 lines give non-sense results.
### These are mostly noble gases. Remove them:
###
summary_matrix = na.omit(summary_matrix)
###
### Now plot them:
###
mean_crit_temp_per_element = summary_matrix[,"Mean"]
off_set = 2
### mar c(bottom, left, top, right) = default is c(5, 4, 4, 2) + 0.1
par(mar = c(2, 5, 2, 1) + 0.1)
x = 1:length(mean_crit_temp_per_element)
plot( x = x, y = mean_crit_temp_per_element, xaxt = "n", xlab = "", ylab = "Mean Critical Temperature (K)",
, pch = 19, cex.lab = 1.25, ylim = c(-5, max(mean_crit_temp_per_element) + off_set * 1.1) + 5, cex = 0.65)
abline(h = seq(from = 0, to = 80, by = 10), lwd = 0.1, col = "grey")
text(x, mean_crit_temp_per_element + off_set, rownames(summary_matrix), cex = 0.65)
###
### The file above is saved in "mean_crit_temp_per_element.pdf".
###
###
### Look at the top 20 as well.
###
### mar c(bottom, left, top, right) = default is c(5, 4, 4, 2) + 0.1
windows(width = 10, height = 7)
par(mar = c(2, 5, 2, 1) + 0.1)
x = 1:length(mean_crit_temp_per_element[1:20])
plot( x = x, y = mean_crit_temp_per_element[1:20], xaxt = "n", xlab = "", ylab = "Mean Critical Temperature (K)",
, pch = 19, cex.lab = 1.25, ylim = c(35, 85), cex = 0.65)
abline(h = seq(from = 35, to = 85, by = 5), lwd = 0.1, col = "grey")
text(x, mean_crit_temp_per_element[1:20] + 2, rownames(summary_matrix)[1:20])
###
### The file above is saved in "mean_crit_temp_per_element_top_20.pdf".
###
###
### Now repeat but sort by standard deviation:
###
summary_matrix = summary_matrix[ order(summary_matrix[,7], decreasing = TRUE) , ]
summary_matrix = as.data.frame(summary_matrix)
###
### Now plot it:
###
sd_crit_temp_per_element = summary_matrix[,"SD"]
off_set = 2
### mar c(bottom, left, top, right) = default is c(5, 4, 4, 2) + 0.1
par(mar = c(2, 5, 2, 1) + 0.1)
x = 1:length(sd_crit_temp_per_element)
plot( x = x, y = sd_crit_temp_per_element, xaxt = "n", xlab = "", ylab = "SD Critical Temperature (K)",
, pch = 19, cex.lab = 1.25, ylim = c(-5, max(sd_crit_temp_per_element) + off_set * 1.1) + 5, cex = 0.65)
abline(h = seq(from = 0, to = 80, by = 5), lwd = 0.1, col = "grey")
text(x, sd_crit_temp_per_element + off_set, rownames(summary_matrix), cex = 0.65)
###
### The plot is saved in "sd_crit_temp_per_element.pdf".
###
###
### Look at the top 20 as well.
###
### mar c(bottom, left, top, right) = default is c(5, 4, 4, 2) + 0.1
windows(width = 10, height = 7)
par(mar = c(2, 5, 2, 1) + 0.1)
x = 1:length(sd_crit_temp_per_element[1:20])
plot( x = x, y = sd_crit_temp_per_element[1:20], xaxt = "n", xlab = "", ylab = "SD Critical Temperature (K)",
, pch = 19, cex.lab = 1.25, ylim = c(30,45), cex = 0.65)
abline(h = seq(from = 30, to = 45, by = 5), lwd = 0.1, col = "grey")
text(x, sd_crit_temp_per_element[1:20] + 0.75, rownames(summary_matrix)[1:20])
###
### The plot is saved in "sd_crit_temp_per_element_top_20.pdf".
###
###
### Now plot the mean and sd
###
windows(width = 9, height = 5)
par(mfrow=c(1,2), mar = c(5,5,3,1) + 0.1)
plot(SD ~ Mean , data = summary_matrix,
xlab = "Mean Critical Temperature (K)", ylab = "SD Critical Temperature (K)", pch = 19, cex.lab = 1)
plot(SD ~ log(Mean) , data = summary_matrix,
xlab = "log(Mean Critical Temperature) (K)", ylab = "SD Critical Temperature (K)", pch = 19, cex.lab = 1)
par(mfrow=c(1,1))
###
### The picture is saved in "sd_vs_mean_crit_temp.pdf".
###
###
### Let's look at the histogram of the critical temp for the data selected
###
par(mfrow = c(1,3))
hist(unique_m$critical_temp, col = "grey", freq = FALSE, xlab = "Critical Temperature (K)", main = "")
boxplot(unique_m$critical_temp, col = "grey", xlab = "Critical Temperature (K)", horizontal = TRUE, pch = 19)
plot(rev(sort(unique_m$critical_temp)), cex = 0.65, pch = 19, ylab = "Critical Temperature (K)", ylim = c(0,200))
abline(h = 0 , lwd = 0.1, lty = 2)
par(mfrow = c(1,1))
###
### The above is saved in "hist_boxplot_crit_temp.pdf".
###
hist(unique_m$critical_temp, col = "grey", freq = FALSE,
xlab = "Critical Temperature (K)", main = "")
###
### The above is saved in "hist_crit_temp.pdf".
###
###
### Let's look at the histogram of critical temp of the rejected
### material.
###
rejected_temps = dat[which(reject == 1), "critical_temp"]
hist(rejected_temps, col = "grey", freq = FALSE, xlab = "Critical Temperature (K)", main = "")
###
### The above is saved in "hist_rejected_crit_temp.pdf".
###
###
### Let's compare the quantiles of the two distributions:
###
quantiles_material_kept = quantile(unique_m$critical_temp, prob = seq(from = 0.01, to = 0.99, by = 0.01), names = F)
quantiles_material_rejected = quantile(rejected_dat$critical_temp, prob = seq(from = 0.01, to = 0.99, by = 0.01), names = F)
par(mar = c(5,5,3,1) + 0.1)
plot(quantiles_material_kept ~ quantiles_material_rejected, pch = 19,
xlab = "Quantiles of Rejected", ylab = "Quantiles of Kept")
abline(a = 0, b = 1, col = "red", lty = 2)
###
### The above is saved in "quantiles_of_kept_vs_rejected.pdf".
###
###
### This does not make it look like they are the same; Critical temps are higher for
### what I have kept!
###
###
### Compare summary measures:
###
round(summary(unique_m$critical_temp),2)
round(summary(rejected_dat$critical_temp),2)
### > round(summary(unique_m$critical_temp),2)
### Min. 1st Qu. Median Mean 3rd Qu. Max.
### 0.00 5.37 20.00 34.42 63.00 185.00
### > round(summary(rejected_dat$critical_temp),2)
### Min. 1st Qu. Median Mean 3rd Qu. Max.
### 0.09 1.36 3.43 12.58 12.50 135.00
###
### Actually, it looks like I am leaving some low temp ones out!
###
###
### Let's look at some values at random:
###
set.seed(8811)
rejected_dat[ sample( 1:nrow(rejected_dat), size = 5, replace = F), ]
###
### I could see some mistakes: D0.018Nb0.982 = what is D?
### Pu1Co1Ga5 has Plutonium, etc.
###
###
### Let's do some clean up:
###
rm(j)
rm(tmp)
rm(x)
###
### Save results up to summary analysis:
###
save.image("visual_and_summary_analysis.RData")
###################################################################################
###################################################################################
###################################################################################
###
### In this next part, I will create the training data.
###
###
### This function is the new definition of standard deviation.
### It is the population sd.
###
### I had some problems with R's sd function.
###
std = function(x)
{
out = sqrt(mean((x - mean(x))^2))
out
}
#############################################################################
###
### This function gets the basic summaries based
### on one variable.
### Note that I will be ignoring the missing values.
###
### y = some property of material such as the atomic mass
### p = proportion or coefficient in the chemical formula
###
get_features = function(y, p)
{
###
### out = index to remove the missing values.
###
out = -which(is.na(y))
if ( length(out) > 0 ) {
new_y = y[out]
new_p = p[out]
} else {
new_y = y
new_p = p
}
###
### Now get the features after NA's have been removed.
###
###
### I also have added absolute value for some measurements because
### some values may be negative.
###
mean_y = mean(new_y)
wtd_mean_y = sum(new_p * new_y)
gmean_y = exp(mean(log(abs(new_y))))
wtd_gmean_y = exp(sum(new_p * log(abs(new_y))))
tmp = abs(new_y)/sum(abs(new_y))
entropy_y = -1 * sum(tmp * log(tmp))
tmp = (new_p * abs(new_y))/sum(new_p * abs(new_y))
wtd_entropy_y = -1 * sum(tmp * log(tmp))
range_y = max(new_y) - min(new_y)
wtd_range_y = max(new_p * new_y) - min(new_p * new_y)
lrange_y = max(log(abs(new_y))) - min(log(abs(new_y)))
wtd_lrange_y = max(new_p * log(abs(new_y))) - min(new_p * log(abs(new_y)))
std_y = std(new_y)
wtd_std_y = sqrt(sum((new_y - wtd_mean_y)^2*new_p))
out = c(mean_y, wtd_mean_y, gmean_y, wtd_gmean_y, entropy_y,
wtd_entropy_y, range_y, wtd_range_y, std_y, wtd_std_y)
out
}
#############################################################################
###
### This is the function that will extract information
###
### x = formula of the material
### ed = element data file. "ed" **MUST** have a column name "Element".
###
extract = function(x, ed)
{
###
### Note that names(ratios) gives the
### listing of the elements.
###
### ratios here, tells us how many of each element is in the
### material formula. These are basically the coefficients
### in front of the elements in the chemical formula.
###
ratios = makeup(x)
prob_ratios = data.frame( prob_ratios = ratios/sum(ratios), Element = names(ratios))
###
### Here's the subset table I create:
###
subset_table = subset(ed, Element %in% names(ratios),
c(Element, AtomicMass, FirstIonizationEnergy, AtomicRadius, Density, ElectronAffinity,
FusionHeat, ThermalConductivity, Valence) )
###
### Merge with the proportons. Very important: the column "Element"
### must be present in both subset_table and prob_ratios!
###
subset_table = merge(subset_table, prob_ratios)
###
### Atomic Mass:
###
AtomicMass_features = get_features(subset_table[,"AtomicMass"], subset_table[,"prob_ratios"])
###
### First Ionization Energy = FIE
###
### Note FIE is highly correlated with Electronegativity
###
FirstIonizationEnergy_features = get_features(subset_table[,"FirstIonizationEnergy"],
subset_table[,"prob_ratios"])
###
### AtomicRadius
### Note: AtomicRadius & CovalentRadius are highly correlated.
###
AtomicRadius_features = get_features(subset_table[,"AtomicRadius"],
subset_table[,"prob_ratios"])
###
### Density
###
Density_features = get_features(subset_table[,"Density"],
subset_table[,"prob_ratios"])
###
### ElectronAffinity
### Note: A small constant 1.5 should have been added to prevent log(0) = -Inf
###
ElectronAffinity_features = get_features(subset_table[,"ElectronAffinity"],
subset_table[,"prob_ratios"])
###
### FusionHeat
###
FusionHeat_features = get_features(subset_table[,"FusionHeat"],
subset_table[,"prob_ratios"])
###
### ThermalConductivity
###
ThermalConductivity_features = get_features(subset_table[,"ThermalConductivity"],
subset_table[,"prob_ratios"])
###
### Valence
###
Valence_features = get_features(subset_table[,"Valence"],
subset_table[,"prob_ratios"])
###
### Output
###
number_of_elements = length(ratios)
out = c(number_of_elements, AtomicMass_features, FirstIonizationEnergy_features,
AtomicRadius_features, Density_features, ElectronAffinity_features,
FusionHeat_features, ThermalConductivity_features, Valence_features)
return(out)
}
#############################################################################
###
### There will be two train data; This first one has the iron and cuprate
### 81 is the initial number of features extracted.
### 82 will be whether iron is in there.
### 83 will be whether it is cuprate.
### 84 will be the response.
###
### train: This is the main file I will use to create the models.
###
train = matrix(0, nrow(unique_m), 84)
colnames(train) = c(
"number_of_elements",
"mean_atomic_mass",
"wtd_mean_atomic_mass",
"gmean_atomic_mass",
"wtd_gmean_atomic_mass",
"entropy_atomic_mass",
"wtd_entropy_atomic_mass",
"range_atomic_mass",
"wtd_range_atomic_mass",
"std_atomic_mass",
"wtd_std_atomic_mass",
"mean_fie",
"wtd_mean_fie",
"gmean_fie",
"wtd_gmean_fie",
"entropy_fie",
"wtd_entropy_fie",
"range_fie",
"wtd_range_fie",
"std_fie",
"wtd_std_fie",
"mean_atomic_radius",
"wtd_mean_atomic_radius",
"gmean_atomic_radius",
"wtd_gmean_atomic_radius",
"entropy_atomic_radius",
"wtd_entropy_atomic_radius",
"range_atomic_radius",
"wtd_range_atomic_radius",
"std_atomic_radius",
"wtd_std_atomic_radius",
"mean_Density",
"wtd_mean_Density",
"gmean_Density",
"wtd_gmean_Density",
"entropy_Density",
"wtd_entropy_Density",
"range_Density",
"wtd_range_Density",
"std_Density",