HELLO!

WELCOME TO MY WEBSITE

Teknik Informatika UIN MAULANA MALIK IBRAHIM MALANG|| Lalu Egiq Fahalik Anggara_220605110066 |kelas C

KALKULUS by Prof. Dr. Suhartono, M.Kom

BAB 6 MENYESUAIKAN FUNGSI KE DATA

Seringkali, Anda memiliki gagasan tentang bentuk fungsi untuk model dan Anda perlu memilih parameter yang akan membuat fungsi model cocok untuk observasi. 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). Ada, seperti yang Anda duga, hubungan yang kuat antara keduanya.

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)

Output dari fitModel()adalah fungsi dari bentuk matematika yang sama seperti yang Anda tentukan dalam argumen pertama (di sini, ccf ~ A * temp + B) dengan nilai numerik tertentu yang diberikan ke parameter untuk membuat fungsi paling cocok dengan data. Bagaimana cara fitModel()mengetahui besaran mana dalam bentuk matematika yang merupakan variabel dan mana yang merupakan parameter? Apa pun yang terkandung dalam data yang digunakan untuk pemasangan adalah variabel (di sini temp); hal-hal lain (di sini, Adan B) adalah parameter.

gf_point(ccf ~ temp, data = Utils) %>%
  slice_plot(f(temp) ~ temp)

Anda dapat menambahkan fungsi lain ke dalam campuran dengan mudah. Misalnya, Anda mungkin berpikir itu sqrt(temp)berhasil di sana. Cobalah!

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

Contoh ini hanya melibatkan satu variabel input. Di seluruh ilmu alam dan sosial, teknik yang sangat penting dan banyak digunakan adalah dengan menggunakan banyak variabel dalam suatu proyeksi. Sebagai ilustrasi, lihatlah data di “used-hondas.csv”harga mobil bekas Honda.

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

Seperti yang Anda lihat, kumpulan data menyertakan variabel Price, Age, dan Mileage. Tampaknya masuk akal untuk berpikir bahwa harga akan tergantung pada jarak tempuh dan usia mobil. Berikut adalah model yang sangat sederhana yang menggunakan kedua variabel:

library(mosaicCalc)
carPrice1 <- fitModel(
  Price ~ A + B * Age + C * Mileage, data = Hondas
)

Anda dapat memplot fungsi yang dipasang:

library(mosaicCalc)
contour_plot(
  carPrice1(Age = age, Mileage = miles) ~ age + miles,
  domain(age=2:8, miles=range(0, 60000)))

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

Latihan 1

contour_plot(
  carPrice2(Age=age, Mileage=miles) ~ age + miles,
  domain(age = range(0, 8), miles = range(0, 60000)))

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, 8), miles = range(0, 60000)))

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

Latihan 3: Tetap dekat dengan data

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)

Kurva dan model linier

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

Latihan 1: Menyesuaikan Polinomial

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

jawaban:

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}

jawaban:

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

Latihan 2: Regresi Berganda

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
  1. Gunakan data ini agar sesuai dengan model ekonomi bahan bakar (variabel mpg): =x0+x1.

Berapa nilai model untuk input 2000 pound? {14.9,19.4,21.1,25.0, 28.8 ,33.9,35.2} MENJAWAB:

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 a. 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

Mengoptimalkan tebakan

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.5)/40000,log(0.5)/20000)))

DAFTAR PUSTAKA

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