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

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
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, 60000)))

carPrice2 <- fitModel(
  Price ~ A + B * Age + C * Mileage + D * Age * Mileage,
  data = Hondas)
contour_plot(
  carPrice2(Age=age, Mileage=miles) ~ age + miles,
  domain(age = range(0, 8), miles = range(0, 60000)))

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

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)
## Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.

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)

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

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

ccfQuad(32)
## [1] 143.1713
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
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