library(mosaicCalc)
## Loading required package: mosaic
## Registered S3 method overwritten by 'mosaic':
## method from
## fortify.SpatialPolygonsDataFrame ggplot2
##
## The 'mosaic' package masks several functions from core packages in order to add
## additional features. The original behavior of these functions should not be affected by this.
##
## Attaching package: 'mosaic'
## The following objects are masked from 'package:dplyr':
##
## count, do, tally
## The following object is masked from 'package:Matrix':
##
## mean
## The following object is masked from 'package:ggplot2':
##
## stat
## The following objects are masked from 'package:stats':
##
## binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
## quantile, sd, t.test, var
## The following objects are masked from 'package:base':
##
## max, mean, min, prod, range, sample, sum
## Loading required package: mosaicCore
##
## Attaching package: 'mosaicCore'
## The following objects are masked from 'package:dplyr':
##
## count, tally
##
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
##
## D
Proses pemilihan parameter untuk mencocokkan pengamatan disebut model fitting .
Sebagai ilustrasi, data dalam file “utilities.csv” mencatat suhu rata-rata setiap bulan (dalam derajat F) serta penggunaan gas alam bulanan (dalam kaki kubik, ccf).
Utils <- read.csv("http://www.mosaic-web.org/go/datasets/utilities.csv")
gf_point(ccf ~ temp, data = Utils) %>%
gf_labs(y = "Natural gas usage (ccf/month)",
x = "Average outdoor temperature (F)")
Dengan data utilitas inputnya adalah suhu, suhu. Output yang akan dimodelkan adalah ccf. Untuk menyesuaikan fungsi model dengan data, dapat menuliskan rumus dengan nama input, parameter, dan output yang sesuai di tempat yang tepat:
f <- fitModel(ccf ~ A * temp + B, data = Utils)
Utilities$fitmodel <- f(Utilities$temp)
Utilities
## month day year temp kwh ccf thermsPerDay billingDays totalbill gasbill
## 1 12 29 1999 26 892 194 5.5 36 173.65 112.72
## 2 1 28 2000 18 533 164 5.6 30 139.18 95.88
## 3 2 26 2000 24 521 228 8.0 29 177.48 134.65
## 4 3 25 2000 41 554 16 0.6 28 61.27 15.32
## 5 4 28 2000 45 638 74 2.2 34 100.33 47.33
## 6 5 30 2000 60 700 129 4.1 32 153.32 89.87
## 7 6 24 2000 66 583 23 0.9 25 85.30 25.55
## 8 7 26 2000 72 935 0 0.0 32 102.44 8.08
## 9 8 24 2000 72 789 13 0.4 29 96.47 17.66
## 10 9 25 2000 64 864 17 0.5 32 104.86 21.39
## 11 10 24 2000 54 778 37 1.3 29 107.50 41.19
## 12 11 26 2000 37 617 123 3.8 33 150.13 102.52
## 13 12 27 2000 11 586 235 7.7 31 254.23 210.87
## 14 6 26 2001 70 160 1 0.1 10 31.55 3.42
## 15 7 26 2001 76 736 7 0.2 30 92.36 12.79
## 16 8 26 2001 75 923 15 0.5 31 114.95 18.10
## 17 9 25 2001 64 865 20 0.7 30 105.91 20.17
## 18 10 24 2001 51 828 44 1.6 29 107.58 32.38
## 19 11 26 2001 48 1046 79 2.4 33 134.50 53.60
## 20 1 28 2002 23 581 210 6.6 32 174.45 127.86
## 21 2 26 2002 28 551 178 6.2 29 147.06 102.85
## 22 3 27 2002 21 471 190 6.6 29 152.32 113.63
## 23 4 28 2002 45 449 106 3.3 32 106.04 70.34
## 24 5 28 2002 51 394 60 2.0 30 87.47 48.92
## 25 6 26 2002 69 496 23 0.8 29 76.43 23.42
## 26 7 28 2002 76 925 16 0.5 32 111.65 18.61
## 27 8 26 2002 72 812 15 0.5 29 101.39 17.56
## 28 9 25 2002 69 838 16 0.5 30 99.46 18.16
## 29 10 24 2002 47 790 69 2.4 29 122.51 55.74
## 30 11 24 2002 34 865 126 4.1 31 154.93 94.67
## 31 12 29 2002 25 1032 190 5.5 35 217.42 140.49
## 32 2 26 2003 17 580 224 7.8 29 232.41 187.05
## 33 3 27 2003 29 648 153 5.3 29 226.92 176.02
## 34 4 28 2003 46 503 100 3.2 32 127.07 86.83
## 35 5 28 2003 56 496 43 1.4 30 92.86 43.77
## 36 6 26 2003 67 722 18 0.6 29 99.52 24.46
## 37 7 28 2003 72 934 15 0.5 32 116.29 21.28
## 38 8 26 2003 75 869 14 0.5 29 108.04 19.56
## 39 9 25 2003 69 888 16 0.5 30 108.54 21.08
## 40 10 26 2003 53 927 48 1.5 31 127.37 45.28
## 41 11 24 2003 35 570 130 4.6 29 151.62 106.61
## 42 12 29 2003 25 725 204 5.9 35 225.73 168.93
## 43 1 28 2004 15 594 242 8.1 30 262.81 216.89
## 44 2 26 2004 16 563 216 7.6 29 239.60 193.45
## 45 3 28 2004 35 510 144 4.7 31 166.51 124.18
## 46 4 27 2004 48 709 78 2.6 30 120.08 65.67
## 47 5 26 2004 58 742 35 1.2 29 109.38 39.40
## 48 6 27 2004 64 911 18 0.6 32 119.65 25.14
## 49 7 27 2004 72 860 8 0.3 30 106.65 15.59
## 50 8 25 2004 67 841 15 0.5 29 111.08 21.72
## 51 9 26 2004 71 922 15 0.5 32 117.46 21.25
## 52 11 23 2004 43 860 82 2.8 29 160.26 88.51
## 53 12 28 2004 23 1160 208 6.0 35 317.47 224.18
## 54 1 27 2005 15 891 224 7.5 30 294.96 223.92
## 55 2 24 2005 29 557 166 6.0 28 213.71 166.63
## 56 3 29 2005 31 772 179 5.5 33 239.85 117.05
## 57 4 28 2005 54 444 61 2.0 30 103.34 64.99
## 58 5 26 2005 56 645 51 1.8 28 127.22 61.81
## 59 6 27 2005 72 939 19 0.6 32 131.02 27.30
## 60 7 27 2005 78 862 11 0.4 30 116.72 19.96
## 61 8 25 2005 74 845 9 0.3 29 120.53 18.16
## 62 9 26 2005 69 995 11 0.3 32 135.07 22.33
## 63 10 25 2005 56 965 32 1.1 29 150.62 55.74
## 64 11 27 2005 41 926 99 3.1 33 212.49 153.24
## 65 12 28 2005 21 931 176 5.8 31 324.52 240.90
## 66 1 29 2006 30 927 144 4.5 32 282.25 193.84
## 67 2 27 2006 22 876 161 5.6 29 289.91 198.11
## 68 3 28 2006 34 749 116 4.0 29 210.85 138.65
## 69 4 26 2006 53 428 52 1.8 29 96.87 55.00
## 70 5 25 2006 59 450 38 1.3 29 95.04 47.39
## 71 6 26 2006 74 694 10 0.3 32 98.48 19.19
## 72 7 26 2006 78 954 7 0.2 30 131.27 16.37
## 73 8 24 2006 77 957 6 0.2 29 134.96 15.88
## 74 9 25 2006 64 1027 15 0.5 32 156.51 25.74
## 75 10 24 2006 50 893 47 1.6 29 144.16 46.12
## 76 11 26 2006 41 663 101 3.1 33 168.24 106.54
## 77 12 27 2006 30 720 140 4.5 31 229.40 159.08
## 78 1 28 2007 24 897 168 5.3 32 267.72 178.16
## 79 2 26 2007 13 808 191 6.7 29 298.50 207.53
## 80 3 26 2007 38 724 101 3.6 29 192.67 118.78
## 81 4 26 2007 46 707 77 2.6 30 159.01 82.76
## 82 5 28 2007 65 442 18 0.6 32 86.54 32.98
## 83 6 26 2007 74 305 7 0.2 29 67.19 21.41
## 84 7 27 2007 76 839 9 0.3 30 135.73 22.87
## 85 8 26 2007 75 809 6 0.2 31 123.07 19.17
## 86 9 25 2007 68 812 13 0.4 30 117.82 24.54
## 87 10 24 2007 58 761 28 1.0 29 123.40 38.59
## 88 11 26 2007 41 767 98 3.0 33 181.53 104.52
## 89 12 27 2007 18 980 182 6.0 31 296.10 194.91
## 90 2 26 2008 15 804 191 6.7 29 292.12 207.32
## 91 3 27 2008 28 752 139 4.7 30 245.27 167.30
## 92 4 27 2008 45 623 79 2.6 31 160.69 97.11
## 93 5 27 2008 55 410 29 1.0 30 105.50 52.15
## 94 6 25 2008 68 196 6 0.2 29 53.92 20.97
## 95 7 27 2008 76 477 11 0.3 32 99.14 69.82
## 96 8 25 2008 75 544 12 0.4 29 103.28 26.83
## 97 9 25 2008 67 746 16 0.5 31 124.82 29.77
## 98 10 26 2008 55 801 32 1.1 31 134.30 41.74
## 99 11 24 2008 39 868 91 3.0 29 186.18 93.60
## 100 12 29 2008 18 1205 199 5.8 35 332.09 209.21
## 101 1 28 2009 9 986 211 7.2 30 330.27 225.72
## 102 2 26 2009 23 870 159 5.6 29 242.12 147.82
## 103 3 29 2009 32 830 134 4.4 31 207.96 114.66
## 104 4 28 2009 47 497 74 2.5 30 113.50 58.03
## 105 5 28 2009 61 436 34 1.2 30 93.09 37.33
## 106 6 28 2009 69 579 19 0.6 31 103.70 24.88
## 107 7 28 2009 71 734 15 0.5 30 122.70 22.86
## 108 8 28 2009 72 774 10 0.3 29 125.37 19.43
## 109 9 27 2009 69 909 18 0.6 32 142.82 23.72
## 110 10 26 2009 45 842 62 2.2 29 158.02 58.38
## 111 11 24 2009 46 826 67 2.3 29 153.68 61.46
## 112 12 30 2009 22 1213 188 5.4 36 283.69 131.49
## 113 1 28 2010 15 992 206 6.9 29 291.10 180.73
## 114 2 28 2010 20 1024 187 6.1 31 268.07 163.62
## 115 3 29 2010 41 923 95 3.3 29 181.82 79.15
## 116 4 27 2010 56 814 31 1.1 29 100.61 29.44
## 117 5 36 2010 60 941 31 1.1 29 151.57 38.29
## elecbill notes
## 1 68.25
## 2 43.30
## 3 42.83
## 4 45.95 bad meter reading
## 5 53.00
## 6 63.45
## 7 59.75
## 8 94.36
## 9 78.81
## 10 83.47
## 11 66.31
## 12 47.61
## 13 46.59
## 14 17.43 transfer back from England
## 15 79.57
## 16 96.85
## 17 85.74
## 18 75.20
## 19 80.90
## 20 46.59
## 21 44.21
## 22 38.69
## 23 35.70
## 24 38.55
## 25 53.01
## 26 93.04
## 27 83.83
## 28 82.20
## 29 66.77
## 30 65.02
## 31 76.93
## 32 45.36
## 33 50.90
## 34 40.24
## 35 49.09
## 36 75.06
## 37 95.01
## 38 89.12
## 39 87.46
## 40 82.09
## 41 45.01
## 42 56.80
## 43 47.37
## 44 46.15
## 45 42.33
## 46 54.41
## 47 69.98
## 48 94.51
## 49 91.06
## 50 89.36
## 51 96.21
## 52 71.75
## 53 93.29
## 54 71.04
## 55 47.08
## 56 62.80
## 57 38.35
## 58 65.41
## 59 103.72
## 60 96.76 high efficiency gas furnace and gas water heater installed
## 61 102.37
## 62 112.74
## 63 94.88
## 64 84.75
## 65 83.62
## 66 90.28
## 67 91.80
## 68 72.20
## 69 41.87
## 70 47.65
## 71 79.32 away for 10 days on vacation
## 72 114.90
## 73 119.30
## 74 130.77
## 75 98.04
## 76 62.72
## 77 70.32
## 78 89.97
## 79 90.97
## 80 73.89
## 81 76.25
## 82 53.56
## 83 45.78
## 84 112.99
## 85 103.90
## 86 98.90 5.46 credit for "cost of gas"
## 87 85.81
## 88 77.01
## 89 101.19
## 90 84.80 housesitters
## 91 77.97 housesitters
## 92 63.58 housesitters
## 93 53.35 housesitters
## 94 32.95 empty house
## 95 29.32 empty house
## 96 76.45
## 97 95.05
## 98 92.56
## 99 92.58
## 100 122.88
## 101 104.55
## 102 94.30
## 103 93.30
## 104 55.47
## 105 55.76
## 106 78.82
## 107 99.84
## 108 105.94 Was this August?
## 109 119.10
## 110 102.52
## 111 92.22
## 112 152.20 estimated reading
## 113 110.37
## 114 114.02 9.57 escrow refund
## 115 102.67
## 116 95.22 24.05 interim elec refund
## 117 113.18
## fitmodel
## 1 163.027680
## 2 190.741689
## 3 169.956182
## 4 111.063913
## 5 97.206909
## 6 45.243143
## 7 24.457636
## 8 3.672130
## 9 3.672130
## 10 31.386138
## 11 66.028649
## 12 124.920918
## 13 214.991446
## 14 10.600632
## 15 -10.184875
## 16 -6.720624
## 17 31.386138
## 18 76.421403
## 19 86.814156
## 20 173.420433
## 21 156.099178
## 22 180.348935
## 23 97.206909
## 24 76.421403
## 25 14.064883
## 26 -10.184875
## 27 3.672130
## 28 14.064883
## 29 90.278407
## 30 135.313671
## 31 166.491931
## 32 194.205940
## 33 152.634927
## 34 93.742658
## 35 59.100147
## 36 20.993385
## 37 3.672130
## 38 -6.720624
## 39 14.064883
## 40 69.492900
## 41 131.849420
## 42 166.491931
## 43 201.134442
## 44 197.670191
## 45 131.849420
## 46 86.814156
## 47 52.171645
## 48 31.386138
## 49 3.672130
## 50 20.993385
## 51 7.136381
## 52 104.135411
## 53 173.420433
## 54 201.134442
## 55 152.634927
## 56 145.706424
## 57 66.028649
## 58 59.100147
## 59 3.672130
## 60 -17.113377
## 61 -3.256372
## 62 14.064883
## 63 59.100147
## 64 111.063913
## 65 180.348935
## 66 149.170675
## 67 176.884684
## 68 135.313671
## 69 69.492900
## 70 48.707394
## 71 -3.256372
## 72 -17.113377
## 73 -13.649126
## 74 31.386138
## 75 79.885654
## 76 111.063913
## 77 149.170675
## 78 169.956182
## 79 208.062944
## 80 121.456667
## 81 93.742658
## 82 27.921887
## 83 -3.256372
## 84 -10.184875
## 85 -6.720624
## 86 17.529134
## 87 52.171645
## 88 111.063913
## 89 190.741689
## 90 201.134442
## 91 156.099178
## 92 97.206909
## 93 62.564398
## 94 17.529134
## 95 -10.184875
## 96 -6.720624
## 97 20.993385
## 98 62.564398
## 99 117.992416
## 100 190.741689
## 101 221.919948
## 102 173.420433
## 103 142.242173
## 104 90.278407
## 105 41.778892
## 106 14.064883
## 107 7.136381
## 108 3.672130
## 109 14.064883
## 110 97.206909
## 111 93.742658
## 112 176.884684
## 113 201.134442
## 114 183.813186
## 115 111.063913
## 116 59.100147
## 117 45.243143
gf_point(ccf ~ temp, data = Utils) %>%
slice_plot(f(temp) ~ temp)
Dapat menambahkan fungsi lain ke dalam campuran. Misalnya, sqrt(temp)
f2 <- fitModel(
ccf ~ A * temp + B + C *sqrt(temp),
data = Utils)
gf_point(
ccf ~ temp, data = Utils) %>%
slice_plot(f2(temp) ~ temp)
Sebagai ilustrasi, data “used-hondas.csv”harga mobil Honda bekas.
Hondas <- read.csv("http://www.mosaic-web.org/go/datasets/used-hondas.csv")
head(Hondas)
## Price Year Mileage Location Color Age
## 1 20746 2006 18394 St.Paul Grey 1
## 2 19787 2007 8 St.Paul Black 0
## 3 17987 2005 39998 St.Paul Grey 2
## 4 17588 2004 35882 St.Paul Black 3
## 5 16987 2004 25306 St.Paul Grey 3
## 6 16987 2005 33399 St.Paul Black 2
model yang yang menggunakan kedua variabel:
carPrice1 <- fitModel(
Price ~ A + B * Age + C * Mileage, data = Hondas
)
dapat memplot fungsi yang dipasang:
contour_plot(
carPrice1(Age = age, Mileage = miles) ~ age + miles,
domain(age=2:8, miles=range(0, 60000)))
Model yang agak lebih canggih mungkin mencakup apa yang disebut interaksi antara usia dan jarak tempuh, mengakui bahwa efek usia mungkin berbeda tergantung pada jarak tempuh.
carPrice2 <- fitModel(
Price ~ A + B * Age + C * Mileage + D * Age * Mileage,
data = Hondas)
memplot dengan cara biasa
contour_plot(
carPrice2(Age=age, Mileage=miles) ~ age + miles,
domain(age = range(0, 8), miles = range(0, 60000)))