Bab 6 Menyesuaikan fungsi ke data

LATIHAN SOAL

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
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)")

library(mosaicCalc)
f <- fitModel(ccf ~ A * temp + B, data = Utils)
gf_point(ccf ~ temp, data = Utils) %>%
  slice_plot(f(temp) ~ temp)

f2 <- fitModel(
  ccf ~ A * temp + B + C *sqrt(temp),
  data = Utils)
gf_point(
  ccf ~ temp, data = Utils) %>%
  slice_plot(f2(temp) ~ temp)

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
library(mosaicCalc)
carPrice1 <- fitModel(
  Price ~ A + B * Age + C * Mileage, data = Hondas
)
contour_plot(
  carPrice1(Age = age, Mileage = miles) ~ age + miles,
  domain(age=2:8, miles=range(0, 70000)))

carPrice2 <- fitModel(
  Price ~ A + B * Age + C * Mileage + D * Age * Mileage,
  data = Hondas)

Latihan 1

library(mosaicCalc)
contour_plot(
  carPrice2(Age=age, Mileage=miles) ~ age + miles,
  domain(age = range(0, 16), miles = range(0, 28000)))

Latihan 2

logPrice2 <- fitModel(
  logPrice ~ A + B * Age + C * Mileage + D * Age * Mileage,
  data = Hondas %>% mutate(logPrice = log10(Price)))
contour_plot(
  logPrice2(Age=age, Mileage=miles) ~ age + miles,
  domain(age = range(0, 10), miles = range(0, 28000)))

contour_plot(
  logPrice2(Age=age, Mileage=miles) ~ age + miles,
  domain(age = range(4, 3), miles = range(0, 4500)))
## Warning: Computation failed in `stat_contour_fill()`:
##  factor level [2] is duplicated

carPrice3 <- fitModel(
  Price ~ A + B * Age + C * Mileage + D * Age * Mileage +
    E * Age^2 + F * Mileage^2 + G * Age^2 * Mileage + 
    H * Age * Mileage^2,
  data = Hondas)
gf_point(Mileage ~ Age, data = Hondas, fill = NA) %>%
contour_plot(
  carPrice3(Age=Age, Mileage=Mileage) ~ Age + Mileage)

Linier Curva

Utilities = read.csv("http://www.mosaic-web.org/go/datasets/utilities.csv")
gf_point(ccf ~ temp, data = Utilities)

project(ccf ~ temp + 1, data = Utilities)
## (Intercept)        temp 
##  253.098208   -3.464251
model_fun = makeFun( 253.098 - 3.464*temp ~ temp)
gf_point(ccf ~ temp, data=Utils) %>%
  slice_plot(model_fun(temp) ~ temp)

project(ccf ~ temp  + sqrt(temp) + 1, data = Utils)
## (Intercept)        temp  sqrt(temp) 
##  447.029273    1.377666  -63.208025
mod2 <- makeFun(447.03 + 1.378*temp - 63.21*sqrt(temp) ~ temp)
gf_point(ccf ~ temp, data=Utils) %>% # the data
  slice_plot(mod2(temp) ~ temp) %>%
  gf_labs(x = "Temperature (F)", 
          y = "Natural gas used (ccf)")

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
project(Price ~ Age + Mileage + 1, data = Hondas)
##   (Intercept)           Age       Mileage 
##  2.133049e+04 -5.382931e+02 -7.668922e-02
car_price <- makeFun(21330-5.383e2*age-7.669e-2*miles ~ age & miles)
contour_plot(car_price(age, miles) ~ age + miles,
  domain(age=range(2, 8), miles=range(0, 60000))) %>%
  gf_labs(title = "Miles per gallon")

project(Price ~ Age + Mileage + Age*Mileage + 1, data = Hondas)
##   (Intercept)           Age       Mileage   Age:Mileage 
##  2.213744e+04 -7.494928e+02 -9.413962e-02  3.450033e-03
car_price2 <- makeFun(22137 - 7.495e2*age - 9.414e-2*miles +
                         3.450e-3*age*miles ~ age & miles)
contour_plot(
  car_price2(Age, Mileage) ~ Age + Mileage,  
  domain(Age = range(0, 10), Mileage = range(0, 100000))) %>%
  gf_labs(title = "Price of car (USD)")

Utilities = read.csv("http://www.mosaic-web.org/go/datasets/utilities.csv")
project(ccf ~ 1 + temp + I(temp^2), data = Utilities)
##  (Intercept)         temp    I(temp^2) 
## 317.58743630  -6.85301947   0.03609138
ccfQuad <- makeFun(317.587 - 6.853*T + 0.0361*T^2 ~ T)
gf_point(ccf ~ temp, data = Utilities) %>%
  slice_plot(ccfQuad(temp) ~ temp) 

ccfQuad(T=72)
## [1] 11.3134

jawab

project(ccf ~ 1 + temp + I(temp^2) + I(temp^3), data = Utils)
##   (Intercept)          temp     I(temp^2)     I(temp^3) 
##  2.550709e+02 -1.427408e+00 -9.643482e-02  9.609511e-04
ccfCubic <- 
  makeFun(2.551e2 - 1.427*T -
          9.643e-2*T^2 + 9.6095e-4*T^3 ~ T)
gf_point(ccf ~ temp, data = Utils) %>%
  slice_plot(ccfCubic(temp) ~ temp) 

ccfCubic(32)
## [1] 142.1801

1 Pasang polinomial orde ke-4 dari ccfversus tempke data utilitas. Berapa nilai model ini untuk suhu 32 derajat? {87.103.128.140, 143 ,168,184}

project(ccf ~ 1 + temp + I(temp^2) + I(temp^3) + I(temp^4), 
        data = Utils)
##   (Intercept)          temp     I(temp^2)     I(temp^3)     I(temp^4) 
##  1.757579e+02  8.225746e+00 -4.815403e-01  7.102673e-03 -3.384490e-05
ccfQuad <- makeFun(1.7576e2 + 8.225*T -4.815e-1*T^2 + 
                     7.103e-3*T^3 - 3.384e-5*T^4 ~ T) 
gf_point(ccf ~ temp, data = Utils) %>%
  slice_plot(ccfQuad(temp) ~ temp) %>%
  gf_labs(y = "Natural gas use (ccf)", x = "Temperature (F)")

library(mosaicCalc)
ccfQuad(32)
## [1] 143.1713
  1. Buat plot perbedaan antara model orde ke-3 dan ke-4 pada rentang suhu dari 20 hingga 60 derajat. Apa perbedaan terbesar (dalam nilai absolut) antara output dari kedua model?
slice_plot(ccfQuad(temp) - ccfCubic(temp) ~ temp, 
           domain(temp = range(20, 60)))

Cars = read.csv("http://www.mosaic-web.org/go/datasets/cardata.csv")
head(Cars)
##    mpg  pounds horsepower cylinders tons constant
## 1 16.9 3967.60        155         8  2.0        1
## 2 15.5 3689.14        142         8  1.8        1
## 3 19.2 3280.55        125         8  1.6        1
## 4 18.5 3585.40        150         8  1.8        1
## 5 30.0 1961.05         68         4  1.0        1
## 6 27.5 2329.60         95         4  1.2        1
project(mpg ~ pounds + 1, data = Cars)
##  (Intercept)       pounds 
## 43.188646127 -0.007200773
43.1886 - 0.00720*2000
## [1] 28.7886

Gunakan data agar sesuai dengan model ekonomi bahan bakar berikut (variabel mpg): =kamu0+kamu1+kamu2. a. Berapa nilai model untuk input 2000 pound dan 150 tenaga kuda? {14.9, 19.4 ,21.1,25.0,28.8,33.9,35.2} b. Berapa nilai model untuk input 2000 pound dan 50 tenaga kuda? {14.9,19.4,21.1,25.0,28.8, 33.9 ,35.2}

jawaban

project(mpg ~ pounds + horsepower  + 1, data = Cars)
##  (Intercept)       pounds   horsepower 
## 46.932738241 -0.002902265 -0.144930546
mod_fun <- makeFun(46.933 - 0.00290*lbs - 0.1449*hp ~ lbs + hp)
mod_fun(lbs = 2000, hp = 50)
## [1] 33.888

Fungsi dengan parameter nonlinier

  1. Fungsi eksponensial
Families <- read.csv("http://www.mosaic-web.org/go/datasets/Income-Housing.csv")
gf_point(TwoVehicles ~ Income, data = Families)

kguess <- log(0.5) / 25000
kguess
## [1] -2.772589e-05
project( TwoVehicles ~ 1 + exp(Income*kguess), data = Families)
##          (Intercept) exp(Income * kguess) 
##             110.4263            -101.5666
f <- makeFun( 110.43 - 101.57*exp(Income * k) ~ Income, k = kguess)
gf_point(TwoVehicles ~ Income, data = Families) %>%
  slice_plot(f(Income) ~ Income) 

f(Income = 10000)
## [1] 33.45433
f(Income = 50000)
## [1] 85.0375
sum_square_resids <- Vectorize(function(k) {
  sum((Families$TwoVehicles - f(Income=Families$Income, k)) ^ 2)
})
slice_plot(
   sum_square_resids(k) ~ k, 
   domain(k = range(log(0.8)/28000,log(0.5)/25000)))

Utilities = read.csv("http://www.mosaic-web.org/go/datasets/utilities.csv")
project(ccf ~ 1 + temp + I(temp^2), data = Utilities)
##  (Intercept)         temp    I(temp^2) 
## 317.58743630  -6.85301947   0.03609138
ccfQuad <- makeFun(317.587 - 6.853*T + 0.0361*T^2 ~ T)
gf_point(ccf ~ temp, data = Utilities) %>%
  slice_plot(ccfQuad(temp) ~ temp) 

a. Pasang polinomial orde ke-3 dari versus ke data utilitas. Berapa nilai model ini untuk suhu 32 derajat? {87.103.128, 142 ,143.168.184}

jawab

ccfCubic <- 
  makeFun(2.551e2 - 1.427*T -
          9.643e-2*T^2 + 9.6095e-4*T^3 ~ T)
gf_point(ccf ~ temp, data = Utils) %>%
  slice_plot(ccfCubic(temp) ~ temp) 

1. Pasang polinomial orde ke-4 dari ccfversus tempke data utilitas. Berapa nilai model ini untuk suhu 32 derajat? {87.103.128.140, 143 ,168,184}

jawab

ccfQuad <- makeFun(1.7576e2 + 8.225*T -4.815e-1*T^2 + 
                     7.103e-3*T^3 - 3.384e-5*T^4 ~ T) 
gf_point(ccf ~ temp, data = Utils) %>%
  slice_plot(ccfQuad(temp) ~ temp) %>%
  gf_labs(y = "Natural gas use (ccf)", x = "Temperature (F)")

REFERENSI

https://dtkaplan.github.io/RforCalculus/index.html?fbclid=IwAR1d_WcAeawvUaBnLKlkRoO2sV4b-6nRX0eNR3DT457DKN7NJV8NV0giSLo