Families <- read.csv("http://www.mosaic-web.org/go/datasets/Income-Housing.csv")
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
gf_point(Telephone ~ IncomePercentile, data = Families)

kguess <- log(0.85) / 45000
kguess
## [1] -3.611532e-06
project( Telephone ~ 1 + exp(IncomePercentile*kguess), data = Families)
##                    (Intercept) exp(IncomePercentile * kguess) 
##                       92966.15                      -92891.77
f <- makeFun( 125.45 - 105.55*exp(IncomePercentile * k) ~ IncomePercentile, k = kguess)
gf_point(Telephone ~ IncomePercentile, data = Families) %>%
  slice_plot(f(IncomePercentile) ~ IncomePercentile) 

Grafik berjalan sangat dekat dengan titik data. Tapi Anda juga bisa melihat nilai numerik dari fungsi untuk setiap pendapatan:

f(IncomePercentile = 100)
## [1] 19.93811
Results <- Families %>% 
  dplyr::select(IncomePercentile, Telephone) %>%
  mutate(model_val = f(IncomePercentile = IncomePercentile),
         resids = Telephone - model_val)
Results
##   IncomePercentile Telephone model_val   resids
## 1                5      68.7  19.90191 48.79809
## 2               15      79.7  19.90572 59.79428
## 3               30      90.8  19.91144 70.88856
## 4               50      96.5  19.91906 76.58094
## 5               70      98.3  19.92668 78.37332
## 6               90      99.5  19.93430 79.56570

Residual adalah perbedaan antara nilai model ini dan nilai sebenarnya dari Telephone kumpulan data. Kolom resids memberikan sisa untuk setiap baris. Tapi Anda juga bisa memikirkan residskolom sebagai vektor . Ingatlah bahwa panjang kuadrat vektor adalah jumlah residu kuadrat

sum(Results$resids^3)
## [1] 2120440

Panjang persegi vektor ini residsmerupakan cara penting untuk mengukur seberapa cocok model dengan data.