Menyesuaikan Fungsi ke Data

seringkali, seseorang memiliki ide tentang bentuk fungsi untuk ragam model. Proses pemilihan parameter untuk mencocokkan pengamatan disebut “Model Fitting”

sebagai ilustrasi data dalam file “utilities.csv”. mecatat suhu rata-rata setiap bulan dalam derajat fahrenheit.

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

Banyak jenis fungsi yang berbeda dapat digunakan untuk mewakili data ini. salah satu fungsi sedeerhana adalah fungsi garis lurus

f(x)=Ax+B

fungsi f(x) variabel x singkatan dari input A dan B adalah parameter.penting untk di input dan output saat meneyesuaikan model dengan data anda perlu mengatur agar namanya cocok dengan data yang sesuai.

dengan data utilitas, masukannya adalah suhu. dalam pemodelan ini akan di modelkan ccf.

f <- fitModel(ccf ~ A * temp + B, data = Utils)

ini adalah contoh model dalam utilitas yang sesuai.

keluaran pada fitmodel() adalah fungsi dengan bentuk matematika yang sama. dengan nilai numerik tertentu yang diberikan ke parameter untuk membuat fungsi.

fitmodel() membuat besaran dalam bentuk matematika dengan menggunakan data yang terkandung atau digunakan dalam pemasangan variabel.

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

yang atas menghasilkan fungsi dengan menghadirkan garis lurus.

library(mosaicCalc)
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)

contoh di atas melibatkan fungsi dalam satu variabel masukan. sepanjang ilmu alam dan sosial, teknik yang sangat penting dan banyak. sebagai ilustrasi, lihat data di “bekas-honda.csv” tentang 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
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)")

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