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
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
## (Intercept)        temp 
##  253.098208   -3.464251
## (Intercept)        temp  sqrt(temp) 
##  447.029273    1.377666  -63.208025
mod2 <- makeFun(447.03 + 1.378*temp - 63.21*sqrt(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
##   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
##   (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
##   (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)")

6.4 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
## [1] -2.772589e-05
##          (Intercept) exp(Income * kguess) 
##             110.4263            -101.5666

MENGOPTIMALKAN TEBAAKAN

```r
sum_square_resids <- Vectorize(function(k) {
  sum((Families$TwoVehicles - f(Income=Families$Income, k)) ^ 2)
})