R Markdown

library(e1071)
data(iris)
set.seed(1234)
rndSample<-sample(1:nrow(iris),100)
str(rndSample)
##  int [1:100] 28 80 101 111 137 133 144 132 98 103 ...
tr<-iris[rndSample,]
ts<-iris[-rndSample,]
s<-svm(Species~.,tr)
ps<-predict(s,ts)
cm<-table(ps,ts$Species)
precision<-100*(1-sum(diag(cm))/sum(cm))
precision  
## [1] 4
s2<-svm(Species~.,tr,cost=10,kernel="polynomial",degree=3)
ps2<-predict(s2,ts)
(cm <- table(ps, ts$Species))
##             
## ps           setosa versicolor virginica
##   setosa         18          0         0
##   versicolor      0         17         1
##   virginica       0          1        13
precision <- 100*(1-sum(diag(cm))/sum(cm)) #the error rate

#try different numbers for cost and degree and see which gives lowest error
s2 <- svm(Species ~., tr, cost=30, kernel = "polynomial", degree = 1)
ps2 <- predict (s2, ts)
cm2 <- table (ps2, ts$Species)
#the error rate
100*(1-sum(diag(cm2))/sum(cm2))
## [1] 0
data(Boston, package='MASS')
set.seed(1234)
sp<-sample(1:nrow(Boston), 354)
tr<-Boston[sp, ]
ts<-Boston[-sp, ]
s1<-svm(medv ~., tr)
ps1<-predict(s1, ts)
mean(abs(ps1-ts$medv))
## [1] 2.158999
s2<-svm(medv~., tr, kernel="radial", cost=15, epsilon=.1, gamma=.04)
ps2<-predict(s2,ts)
mean(abs(ps2-ts$medv))
## [1] 1.904667
library(readr)
letterdata <- read_csv("C:/Users/alviy/Downloads/letterdata.csv")
## Rows: 20000 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): letter
## dbl (16): xbox, ybox, width, height, onpix, xbar, ybar, x2bar, y2bar, xybar,...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(letterdata)

library(kernlab)
str(letterdata)
## spc_tbl_ [20,000 × 17] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ letter: chr [1:20000] "T" "I" "D" "N" ...
##  $ xbox  : num [1:20000] 2 5 4 7 2 4 4 1 2 11 ...
##  $ ybox  : num [1:20000] 8 12 11 11 1 11 2 1 2 15 ...
##  $ width : num [1:20000] 3 3 6 6 3 5 5 3 4 13 ...
##  $ height: num [1:20000] 5 7 8 6 1 8 4 2 4 9 ...
##  $ onpix : num [1:20000] 1 2 6 3 1 3 4 1 2 7 ...
##  $ xbar  : num [1:20000] 8 10 10 5 8 8 8 8 10 13 ...
##  $ ybar  : num [1:20000] 13 5 6 9 6 8 7 2 6 2 ...
##  $ x2bar : num [1:20000] 0 5 2 4 6 6 6 2 2 6 ...
##  $ y2bar : num [1:20000] 6 4 6 6 6 9 6 2 6 2 ...
##  $ xybar : num [1:20000] 6 13 10 4 6 5 7 8 12 12 ...
##  $ x2ybar: num [1:20000] 10 3 3 4 5 6 6 2 4 1 ...
##  $ xy2bar: num [1:20000] 8 9 7 10 9 6 6 8 8 9 ...
##  $ xedge : num [1:20000] 0 2 3 6 1 0 2 1 1 8 ...
##  $ xedgey: num [1:20000] 8 8 7 10 7 8 8 6 6 1 ...
##  $ yedge : num [1:20000] 0 4 3 2 5 9 7 2 1 1 ...
##  $ yedgex: num [1:20000] 8 10 9 8 10 7 10 7 7 8 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   letter = col_character(),
##   ..   xbox = col_double(),
##   ..   ybox = col_double(),
##   ..   width = col_double(),
##   ..   height = col_double(),
##   ..   onpix = col_double(),
##   ..   xbar = col_double(),
##   ..   ybar = col_double(),
##   ..   x2bar = col_double(),
##   ..   y2bar = col_double(),
##   ..   xybar = col_double(),
##   ..   x2ybar = col_double(),
##   ..   xy2bar = col_double(),
##   ..   xedge = col_double(),
##   ..   xedgey = col_double(),
##   ..   yedge = col_double(),
##   ..   yedgex = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
letters <- letterdata
letters$letter <- as.factor(letters$letter)
str(letters)
## spc_tbl_ [20,000 × 17] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ letter: Factor w/ 26 levels "A","B","C","D",..: 20 9 4 14 7 19 2 1 10 13 ...
##  $ xbox  : num [1:20000] 2 5 4 7 2 4 4 1 2 11 ...
##  $ ybox  : num [1:20000] 8 12 11 11 1 11 2 1 2 15 ...
##  $ width : num [1:20000] 3 3 6 6 3 5 5 3 4 13 ...
##  $ height: num [1:20000] 5 7 8 6 1 8 4 2 4 9 ...
##  $ onpix : num [1:20000] 1 2 6 3 1 3 4 1 2 7 ...
##  $ xbar  : num [1:20000] 8 10 10 5 8 8 8 8 10 13 ...
##  $ ybar  : num [1:20000] 13 5 6 9 6 8 7 2 6 2 ...
##  $ x2bar : num [1:20000] 0 5 2 4 6 6 6 2 2 6 ...
##  $ y2bar : num [1:20000] 6 4 6 6 6 9 6 2 6 2 ...
##  $ xybar : num [1:20000] 6 13 10 4 6 5 7 8 12 12 ...
##  $ x2ybar: num [1:20000] 10 3 3 4 5 6 6 2 4 1 ...
##  $ xy2bar: num [1:20000] 8 9 7 10 9 6 6 8 8 9 ...
##  $ xedge : num [1:20000] 0 2 3 6 1 0 2 1 1 8 ...
##  $ xedgey: num [1:20000] 8 8 7 10 7 8 8 6 6 1 ...
##  $ yedge : num [1:20000] 0 4 3 2 5 9 7 2 1 1 ...
##  $ yedgex: num [1:20000] 8 10 9 8 10 7 10 7 7 8 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   letter = col_character(),
##   ..   xbox = col_double(),
##   ..   ybox = col_double(),
##   ..   width = col_double(),
##   ..   height = col_double(),
##   ..   onpix = col_double(),
##   ..   xbar = col_double(),
##   ..   ybar = col_double(),
##   ..   x2bar = col_double(),
##   ..   y2bar = col_double(),
##   ..   xybar = col_double(),
##   ..   x2ybar = col_double(),
##   ..   xy2bar = col_double(),
##   ..   xedge = col_double(),
##   ..   xedgey = col_double(),
##   ..   yedge = col_double(),
##   ..   yedgex = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
letters_train <- letters[1:16000, ]
letters_test <- letters[16001:20000, ]
letter_classifier <- ksvm(letter~., data=letters_train, kernel="vanilladot") #~., means predict response against all the variables
##  Setting default kernel parameters
letter_predictions<-predict(letter_classifier, letters_test)
head(letter_predictions)
## [1] U N V X N H
## Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
table(letter_predictions, letters_test$letter)
##                   
## letter_predictions   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O
##                  A 144   0   0   0   0   0   0   0   0   1   0   0   1   2   2
##                  B   0 121   0   5   2   0   1   2   0   0   1   0   1   0   0
##                  C   0   0 120   0   4   0  10   2   2   0   1   3   0   0   2
##                  D   2   2   0 156   0   1   3  10   4   3   4   3   0   5   5
##                  E   0   0   5   0 127   3   1   1   0   0   3   4   0   0   0
##                  F   0   0   0   0   0 138   2   2   6   0   0   0   0   0   0
##                  G   1   1   2   1   9   2 123   2   0   0   1   2   1   0   1
##                  H   0   0   0   1   0   1   0 102   0   2   3   2   3   4  20
##                  I   0   1   0   0   0   1   0   0 141   8   0   0   0   0   0
##                  J   0   1   0   0   0   1   0   2   5 128   0   0   0   0   1
##                  K   1   1   9   0   0   0   2   5   0   0 118   0   0   2   0
##                  L   0   0   0   0   2   0   1   1   0   0   0 133   0   0   0
##                  M   0   0   1   1   0   0   1   1   0   0   0   0 135   4   0
##                  N   0   0   0   0   0   1   0   1   0   0   0   0   0 145   0
##                  O   1   0   2   1   0   0   1   2   0   1   0   0   0   1  99
##                  P   0   0   0   1   0   2   1   0   0   0   0   0   0   0   2
##                  Q   0   0   0   0   0   0   8   2   0   0   0   3   0   0   3
##                  R   0   7   0   0   1   0   3   8   0   0  13   0   0   1   1
##                  S   1   1   0   0   1   0   3   0   1   1   0   1   0   0   0
##                  T   0   0   0   0   3   2   0   0   0   0   1   0   0   0   0
##                  U   1   0   3   1   0   0   0   2   0   0   0   0   0   0   1
##                  V   0   0   0   0   0   1   3   4   0   0   0   0   1   2   1
##                  W   0   0   0   0   0   0   1   0   0   0   0   0   2   0   0
##                  X   0   1   0   0   2   0   0   1   3   0   1   6   0   0   1
##                  Y   3   0   0   0   0   0   0   1   0   0   0   0   0   0   0
##                  Z   2   0   0   0   1   0   0   0   3   4   0   0   0   0   0
##                   
## letter_predictions   P   Q   R   S   T   U   V   W   X   Y   Z
##                  A   0   5   0   1   1   1   0   1   0   0   1
##                  B   2   2   3   5   0   0   2   0   1   0   0
##                  C   0   0   0   0   0   0   0   0   0   0   0
##                  D   3   1   4   0   0   0   0   0   3   3   1
##                  E   0   2   0  10   0   0   0   0   2   0   3
##                  F  16   0   0   3   0   0   1   0   1   2   0
##                  G   2   8   2   4   3   0   0   0   1   0   0
##                  H   0   2   3   0   3   0   2   0   0   1   0
##                  I   1   0   0   3   0   0   0   0   5   1   1
##                  J   1   3   0   2   0   0   0   0   1   0   6
##                  K   1   0   7   0   1   3   0   0   5   0   0
##                  L   0   1   0   5   0   0   0   0   0   0   1
##                  M   0   0   0   0   0   3   0   8   0   0   0
##                  N   0   0   3   0   0   1   0   2   0   0   0
##                  O   3   3   0   0   0   3   0   0   0   0   0
##                  P 130   0   0   0   0   0   0   0   0   1   0
##                  Q   1 124   0   5   0   0   0   0   0   2   0
##                  R   1   0 138   0   1   0   1   0   0   0   0
##                  S   0  14   0 101   3   0   0   0   2   0  10
##                  T   0   0   0   3 133   1   0   0   0   2   2
##                  U   0   0   0   0   0 152   0   0   1   1   0
##                  V   0   3   1   0   0   0 126   1   0   4   0
##                  W   0   0   0   0   0   4   4 127   0   0   0
##                  X   0   0   0   1   0   0   0   0 137   1   1
##                  Y   7   0   0   0   3   0   0   0   0 127   0
##                  Z   0   0   0  18   3   0   0   0   0   0 132
#diagonal values indicate the total number of records where the predicted letter matches the true value
agreement<-letter_predictions==letters_test$letter
table(agreement)
## agreement
## FALSE  TRUE 
##   643  3357
prop.table(table(agreement))
## agreement
##   FALSE    TRUE 
## 0.16075 0.83925
letter_classifier_rbf<-ksvm(letter~.,data=letters_train, kernel="rbfdot")
letter_predictions_rbf<-predict(letter_classifier_rbf, letters_test)

library(readxl)
AgeHeight <- read_excel("C:/Users/alviy/Downloads/AgeHeight.xlsx")
#AgeHeight <- read_excel("AgeHeight.xlsx",sheet="Hoja2")#Rename sheet
View(AgeHeight)
lmHeight=lm(height~age, data=AgeHeight)
summary(lmHeight)
## 
## Call:
## lm(formula = height ~ age, data = AgeHeight)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.27238 -0.24248 -0.02762  0.16014  0.47238 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  64.9283     0.5084  127.71  < 2e-16 ***
## age           0.6350     0.0214   29.66 4.43e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.256 on 10 degrees of freedom
## Multiple R-squared:  0.9888, Adjusted R-squared:  0.9876 
## F-statistic:   880 on 1 and 10 DF,  p-value: 4.428e-11
lmHeight2=lm(height~age+no_siblings, data=AgeHeight)
summary(lmHeight2)
## 
## Call:
## lm(formula = height ~ age + no_siblings, data = AgeHeight)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.26297 -0.22462 -0.02021  0.16102  0.49752 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 64.90554    0.53526 121.260 8.96e-16 ***
## age          0.63751    0.02340  27.249 5.85e-10 ***
## no_siblings -0.01772    0.04735  -0.374    0.717    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2677 on 9 degrees of freedom
## Multiple R-squared:  0.9889, Adjusted R-squared:  0.9865 
## F-statistic: 402.2 on 2 and 9 DF,  p-value: 1.576e-09
library(readr)
insurance <- read.csv("C:/Users/alviy/Downloads/insurance.csv", stringsAsFactors=TRUE)
View(insurance)
str(insurance)
## 'data.frame':    1338 obs. of  7 variables:
##  $ age     : int  19 18 28 33 32 31 46 37 37 60 ...
##  $ sex     : Factor w/ 2 levels "female","male": 1 2 2 2 2 1 1 1 2 1 ...
##  $ bmi     : num  27.9 33.8 33 22.7 28.9 ...
##  $ children: int  0 1 3 0 0 0 1 3 2 0 ...
##  $ smoker  : Factor w/ 2 levels "no","yes": 2 1 1 1 1 1 1 1 1 1 ...
##  $ region  : Factor w/ 4 levels "northeast","northwest",..: 4 3 3 2 2 3 3 2 1 2 ...
##  $ charges : num  16885 1726 4449 21984 3867 ...
summary(insurance)
##       age            sex           bmi           children     smoker    
##  Min.   :18.00   female:662   Min.   :15.96   Min.   :0.000   no :1064  
##  1st Qu.:27.00   male  :676   1st Qu.:26.30   1st Qu.:0.000   yes: 274  
##  Median :39.00                Median :30.40   Median :1.000             
##  Mean   :39.21                Mean   :30.66   Mean   :1.095             
##  3rd Qu.:51.00                3rd Qu.:34.69   3rd Qu.:2.000             
##  Max.   :64.00                Max.   :53.13   Max.   :5.000             
##        region       charges     
##  northeast:324   Min.   : 1122  
##  northwest:325   1st Qu.: 4740  
##  southeast:364   Median : 9382  
##  southwest:325   Mean   :13270  
##                  3rd Qu.:16640  
##                  Max.   :63770
hist(insurance$charges)

table(insurance$charges)
## 
##   1121.8739   1131.5066   1135.9407   1136.3994    1137.011   1137.4697 
##           1           1           1           1           1           1 
##   1141.4451   1146.7966   1149.3959   1163.4627    1241.565     1242.26 
##           1           1           1           1           1           1 
##    1242.816    1252.407    1253.936    1256.299    1261.442    1261.859 
##           1           1           1           1           1           1 
##    1263.249   1391.5287   1515.3449    1526.312   1532.4697   1534.3045 
##           1           1           1           1           1           1 
##   1607.5101   1615.7667   1621.3402   1621.8827   1622.1885  1625.43375 
##           1           1           1           1           1           1 
##  1627.28245   1628.4709   1629.8335   1631.6683   1631.8212  1632.03625 
##           1           1           1           1           1           1 
##  1632.56445   1633.0444   1633.9618   1634.5734  1635.73365   1639.5631 
##           1           1           1           1           1           2 
##   1646.4297   1664.9996   1674.6323    1682.597   1694.7964   1702.4553 
##           1           1           1           1           1           1 
##   1704.5681  1704.70015   1705.6245   1708.0014  1708.92575   1711.0268 
##           1           1           1           1           1           1 
##    1712.227   1719.4363   1720.3537   1725.5523     1727.54    1727.785 
##           1           1           1           1           1           1 
##    1728.897    1731.677    1737.376    1743.214    1744.465    1748.774 
##           1           1           1           1           1           1 
##    1759.338  1769.53165   1815.8759   1824.2854    1826.843    1832.094 
##           1           1           1           1           1           1 
##    1837.237   1837.2819    1842.519    1875.344   1877.9294     1880.07 
##           1           1           1           1           1           1 
##    1880.487  1906.35825  1909.52745   1917.3184     1964.78   1967.0227 
##           1           1           1           1           1           1 
##    1969.614     1972.95    1977.815     1980.07   1981.5819   1984.4533 
##           1           1           1           1           1           1 
##   1986.9334    2007.945    2020.177   2020.5523   2026.9741  2045.68525 
##           1           1           1           1           1           1 
##   2055.3249   2102.2647     2103.08   2104.1134  2117.33885  2128.43105 
##           1           1           1           1           1           1 
##   2130.6759   2134.9015  2136.88225   2137.6536   2138.0707    2150.469 
##           1           1           1           1           1           1 
##    2154.361   2155.6815   2156.7518    2166.732   2196.4732  2198.18985 
##           1           1           1           1           1           1 
##  2200.83085   2201.0971  2203.47185  2203.73595   2205.9808  2207.69745 
##           1           1           1           1           1           1 
##  2211.13075  2217.46915   2217.6012   2219.4451  2221.56445   2250.8352 
##           1           1           1           1           1           1 
##   2254.7967  2257.47525   2261.5688      2302.3   2304.0022   2322.6218 
##           1           1           1           1           1           1 
##    2331.519  2352.96845  2362.22905  2395.17155   2396.0959   2404.7338 
##           1           1           1           1           1           1 
##    2416.955   2438.0552  2457.21115    2457.502   2459.7201   2464.6188 
##           1           1           1           1           1           1 
##   2473.3341   2480.9791    2483.736    2494.022   2497.0383   2498.4144 
##           1           1           1           1           1           1 
##   2523.1695  2527.81865  2534.39375   2566.4707    2585.269  2585.85065 
##           1           1           1           1           1           1 
##    2597.779    2632.992   2639.0429   2643.2685   2680.9493   2689.4954 
##           1           1           1           1           1           1 
##   2690.1138  2699.56835   2709.1119  2709.24395  2710.82855  2719.27975 
##           1           1           1           1           1           1 
##   2721.3208   2727.3951  2730.10785   2731.9122    2741.948  2755.02095 
##           1           1           1           1           1           1 
##  2775.19215   2789.0574   2801.2588  2803.69785  2842.76075  2850.68375 
##           1           1           1           1           1           1 
##  2855.43755    2866.091   2867.1196   2897.3235  2899.48935   2902.9065 
##           1           1           1           1           1           1 
##    2904.088    2913.569   2927.0647    2974.126  3021.80915   3044.2133 
##           1           1           1           1           1           1 
##    3046.062   3056.3881  3062.50825   3070.8087   3077.0955    3161.454 
##           1           1           1           1           1           1 
##  3167.45585   3171.6149    3172.018   3176.2877   3176.8159   3180.5101 
##           1           1           1           1           1           1 
##  3201.24515  3206.49135    3208.787  3213.62205   3227.1211   3238.4357 
##           1           1           1           1           1           1 
##    3260.199  3268.84665    3277.161  3279.86855  3292.52985   3309.7926 
##           1           1           1           1           1           1 
##    3353.284   3353.4703   3366.6697     3378.91  3385.39915   3392.3652 
##           1           1           1           1           1           1 
##   3392.9768  3393.35635    3410.324    3443.064   3471.4096    3481.868 
##           1           1           1           1           1           1 
##    3484.331   3490.5491   3500.6123    3537.703    3554.203   3556.9223 
##           1           1           1           1           1           1 
##  3558.62025   3561.8889    3577.999   3579.8287     3591.48  3594.17085 
##           1           1           1           1           1           1 
##    3597.596   3645.0894    3659.346    3693.428   3704.3545   3732.6251 
##           1           1           1           1           1           1 
##   3736.4647   3756.6216   3757.8448    3761.292   3766.8838    3847.674 
##           1           1           1           1           1           1 
##  3857.75925  3861.20965   3866.8552   3875.7341  3877.30425    3906.127 
##           1           1           1           1           1           1 
##   3925.7582   3935.1799   3943.5954   3947.4131  3956.07145   3972.9247 
##           1           1           1           1           1           1 
##   3981.9768    3987.926    3989.841   3994.1778   4005.4225   4032.2407 
##           1           1           1           1           1           1 
##  4040.55825   4058.1161  4058.71245   4074.4537    4076.497  4133.64165 
##           1           1           1           1           1           1 
##  4134.08245   4137.5227    4149.736   4151.0287   4185.0979   4189.1131 
##           1           1           1           1           1           1 
##    4234.927  4237.12655  4239.89265  4243.59005    4260.744   4266.1658 
##           1           1           1           1           1           1 
##   4296.2712  4320.41085   4337.7352   4340.4409  4347.02335    4349.462 
##           1           1           1           1           1           1 
##   4350.5144  4357.04365    4391.652    4399.731    4402.233   4415.1588 
##           1           1           1           1           1           1 
##  4428.88785   4433.3877   4433.9159   4435.0942   4438.2634  4441.21315 
##           1           1           1           1           1           1 
##    4449.462  4454.40265   4462.7218   4463.2051   4466.6214  4500.33925 
##           1           1           1           1           1           1 
##   4504.6624  4518.82625  4527.18295    4529.477    4536.259   4544.2348 
##           1           1           1           1           1           1 
##   4561.1885   4562.8421  4564.19145  4571.41305   4618.0799    4646.759 
##           1           1           1           1           1           1 
##  4661.28635  4667.60765     4670.64   4673.3922   4686.3887    4687.797 
##           1           1           1           1           1           1 
##  4718.20355  4719.52405  4719.73655   4738.2682    4746.344   4747.0529 
##           1           1           1           1           1           1 
##     4751.07   4753.6368    4762.329    4766.022   4779.6023   4795.6568 
##           1           1           1           1           1           1 
##  4827.90495     4830.63   4837.5823  4846.92015  4877.98105    4883.866 
##           1           1           1           1           1           1 
##   4889.0368   4889.9995   4894.7533  4906.40965  4915.05985   4922.9159 
##           1           1           1           1           1           1 
##    4931.647    4934.705   4949.7587   4992.3764   5002.7827    5003.853 
##           1           1           1           1           1           1 
##    5012.471   5028.1466  5031.26955    5080.096   5116.5004   5124.1887 
##           1           1           1           1           1           1 
##   5125.2157   5138.2567   5148.5526    5152.134  5209.57885  5227.98875 
##           1           1           1           1           1           1 
##    5240.765   5245.2269    5246.047    5253.524  5257.50795  5261.46945 
##           1           1           1           1           1           1 
##   5266.3656  5267.81815   5272.1758  5312.16985    5325.651  5327.40025 
##           1           1           1           1           1           1 
##  5354.07465  5373.36425    5375.038   5377.4578    5383.536   5385.3379 
##           1           1           1           1           1           1 
##   5397.6167   5400.9805   5415.6612  5425.02335   5428.7277   5438.7491 
##           1           1           1           1           1           1 
##  5458.04645   5469.0066    5472.449   5478.0368   5484.4673    5488.262 
##           1           1           1           1           1           1 
##   5584.3057   5594.8455    5615.369  5630.45785    5649.715    5662.225 
##           1           1           1           1           1           1 
##   5693.4305   5699.8375    5708.867   5709.1644   5729.0053  5757.41345 
##           1           1           1           1           1           1 
##   5836.5204   5846.9176   5855.9025    5910.944   5920.1041    5926.846 
##           1           1           1           1           1           1 
##   5934.3798   5966.8874    5969.723    5972.378   5974.3847   5976.8311 
##           1           1           1           1           1           1 
##    5979.731  5989.52365    6059.173  6067.12675   6079.6715    6082.405 
##           1           1           1           1           1           1 
##  6112.35295  6113.23105   6117.4945   6123.5688  6128.79745   6184.2994 
##           1           1           1           1           1           1 
##   6185.3208    6186.127    6196.448   6198.7518  6203.90175    6238.298 
##           1           1           1           1           1           1 
##    6250.435   6272.4772    6282.235   6289.7549    6311.952    6313.759 
##           1           1           1           1           1           1 
##  6334.34355   6338.0756   6356.2707  6358.77645   6360.9936  6373.55735 
##           1           1           1           1           1           1 
##  6389.37785  6393.60345  6402.29135   6406.4107    6414.178   6435.6237 
##           1           1           1           1           1           1 
##  6455.86265   6457.8434    6474.013    6496.886   6500.2359  6548.19505 
##           1           1           1           1           1           1 
##   6551.7501  6555.07035  6571.02435    6571.544   6593.5083  6600.20595 
##           1           1           1           1           1           1 
##    6600.361   6610.1097  6640.54485   6652.5288   6653.7886  6664.68595 
##           1           1           1           1           1           1 
##    6666.243   6686.4313   6710.1919   6746.7425   6748.5912    6753.038 
##           1           1           1           1           1           1 
##   6770.1925    6775.961   6781.3542  6796.86325    6799.458   6837.3687 
##           1           1           1           1           1           1 
##    6849.026   6858.4796    6875.961   6877.9801  6933.24225  6940.90985 
##           1           1           1           1           1           1 
##   6948.7008  6985.50695    6986.697    7045.499   7046.7222   7050.0213 
##           1           1           1           1           1           1 
##    7050.642   7077.1894   7133.9025  7144.86265    7147.105   7147.4728 
##           1           1           1           1           1           1 
##    7151.092   7152.6714   7153.5539    7160.094   7160.3303   7162.0122 
##           1           1           1           1           1           1 
##  7173.35995    7196.867  7201.70085   7209.4918  7222.78625  7228.21565 
##           1           1           1           1           1           1 
##   7243.8136   7256.7231    7261.741   7265.7025   7281.5056 7323.734819 
##           1           1           1           1           1           1 
##   7325.0482    7337.748    7345.084   7345.7266    7348.142  7358.17565 
##           1           1           1           1           1           1 
##    7371.772    7418.522   7419.4779  7421.19455    7441.053    7441.501 
##           1           1           1           1           1           1 
##  7443.64305    7445.918  7448.40395    7512.267  7518.02535  7526.70645 
##           1           1           1           1           1           1 
##   7537.1639    7623.518     7624.63    7626.993   7633.7206  7639.41745 
##           1           1           1           1           1           1 
##   7640.3092  7650.77375     7682.67    7726.854   7727.2532  7729.64575 
##           1           1           1           1           1           1 
##   7731.4271  7731.85785    7740.337   7742.1098   7749.1564    7789.635 
##           1           1           1           1           1           1 
##   7804.1605  7935.29115    7954.517    7985.815  7986.47525  8017.06115 
##           1           1           1           1           1           1 
##  8023.13545   8026.6666    8027.968   8059.6791    8062.764    8068.185 
##           1           1           1           1           1           1 
##   8083.9198  8116.26885     8116.68   8124.4084   8125.7845  8162.71625 
##           1           1           1           1           1           1 
##   8211.1002   8219.2039   8232.6388   8233.0975   8240.5896   8252.2843 
##           1           1           1           1           1           1 
##    8269.044    8277.523   8280.6227   8283.6807  8302.53565  8310.83915 
##           1           1           1           1           1           1 
##  8334.45755   8334.5896  8342.90875   8347.1643  8410.04685  8413.46305 
##           1           1           1           1           1           1 
##   8428.0693    8442.667    8444.474    8457.818   8515.7587    8516.829 
##           1           1           1           1           1           1 
##    8520.026    8522.003    8527.532   8534.6718  8538.28845    8539.671 
##           1           1           1           1           1           1 
##   8547.6913    8551.347    8556.907   8569.8618   8582.3023   8596.8278 
##           1           1           1           1           1           1 
##   8601.3293   8603.8234  8604.48365   8605.3615   8606.2174      8615.3 
##           1           1           1           1           1           1 
##   8627.5411  8671.19125  8688.85885    8703.456  8733.22925    8765.249 
##           1           1           1           1           1           1 
##    8782.469    8798.593    8823.279  8823.98575    8825.086   8827.2099 
##           1           1           1           1           1           1 
##  8835.26495   8871.1517   8891.1395  8930.93455    8932.084   8944.1151 
##           1           1           1           1           1           1 
##  8964.06055  8965.79575     8968.33   8978.1851  8988.15875   9048.0273 
##           1           1           1           1           1           1 
##   9058.7303  9095.06825    9101.798    9140.951    9144.565  9174.13565 
##           1           1           1           1           1           1 
##     9182.17   9193.8385   9222.4026   9225.2564   9249.4952    9264.797 
##           1           1           1           1           1           1 
##   9282.4806    9283.562   9288.0267   9290.1395  9301.89355   9304.7019 
##           1           1           1           1           1           1 
##   9361.3268   9377.9047   9386.1613    9391.346    9411.005     9414.92 
##           1           1           1           1           1           1 
##   9432.9253  9447.25035   9447.3824   9487.6442  9500.57305   9504.3103 
##           1           1           1           1           1           1 
##  9541.69555   9549.5651    9563.029   9566.9909   9583.8933  9617.66245 
##           1           1           1           1           1           1 
##   9620.3307     9625.92    9630.397    9634.538   9644.2525  9704.66805 
##           1           1           1           1           1           1 
##    9715.841   9722.7695     9724.53   9748.9106   9778.3472   9788.8659 
##           1           1           1           1           1           1 
##   9800.8882    9850.432   9855.1314    9861.025   9863.4718  9866.30485 
##           1           1           1           1           1           1 
##   9869.8102    9872.701   9875.6804   9877.6077    9880.068  9910.35985 
##           1           1           1           1           1           1 
##   9957.7216     9964.06  9991.03765   10043.249   10065.413 10072.05505 
##           1           1           1           1           1           1 
##   10085.846    10096.97 10106.13425  10107.2206 10115.00885   10118.424 
##           1           1           1           1           1           1 
##  10141.1362  10156.7832  10197.7722   10214.636  10226.2842  10231.4999 
##           1           1           1           1           1           1 
##  10264.4421    10269.46   10325.206  10338.9316   10355.641 10370.91255 
##           1           1           1           1           1           1 
##  10381.4787 10407.08585 10422.91665 10435.06525   10436.096   10450.552 
##           1           1           1           1           1           1 
##  10461.9794  10493.9458  10560.4917  10564.8845   10577.087   10579.711 
##           1           1           1           1           1           1 
##  10594.2257 10594.50155  10600.5483   10601.412 10601.63225   10602.385 
##           1           1           1           1           1           1 
##  10702.6424    10704.47   10713.644 10736.87075    10791.96 10795.93733 
##           1           1           1           1           1           1 
## 10796.35025  10797.3362   10806.839  10807.4863  10825.2537  10848.1343 
##           1           1           1           1           1           1 
##  10923.9332   10928.849 10942.13205    10959.33  10959.6947   10965.446 
##           1           1           1           1           1           1 
## 10976.24575  10977.2063  10982.5013  11013.7119  11015.1747  11033.6617 
##           1           1           1           1           1           1 
##   11070.535   11073.176  11082.5772  11085.5868  11090.7178  11093.6229 
##           1           1           1           1           1           1 
##    11150.78   11163.568 11165.41765  11187.6567  11244.3769   11253.421 
##           1           1           1           1           1           1 
##   11264.541 11272.33139  11286.5387 11289.10925   11299.343 11305.93455 
##           1           1           1           1           1           1 
## 11326.71487   11345.519  11353.2276  11356.6609   11362.755  11363.2832 
##           1           1           1           1           1           1 
##   11365.952  11381.3254 11394.06555  11396.9002   11411.685 11436.73815 
##           1           1           1           1           1           1 
##  11454.0215    11455.28 11482.63485 11488.31695   11512.405 11520.09985 
##           1           1           1           1           1           1 
## 11534.87265   11538.421   11552.904  11554.2236 11566.30055    11576.13 
##           1           1           1           1           1           1 
##  11657.7189 11658.11505 11658.37915    11674.13  11729.6795 11735.87905 
##           1           1           1           1           1           1 
## 11737.84884   11741.726   11743.299  11743.9341  11763.0009  11830.6072 
##           1           1           1           1           1           1 
##  11833.7823    11837.16 11840.77505   11842.442 11842.62375   11848.141 
##           1           1           1           1           1           1 
##  11856.4115 11879.10405   11881.358  11881.9696 11884.04858 11931.12525 
##           1           1           1           1           1           1 
## 11938.25595 11944.59435  11945.1327  11946.6259  11987.1682  12029.2867 
##           1           1           1           1           1           1 
##   12032.326   12044.342   12094.478  12096.6512    12105.32  12124.9924 
##           1           1           1           1           1           1 
## 12129.61415  12142.5786   12146.971  12222.8983 12224.35085  12231.6136 
##           1           1           1           1           1           1 
##   12233.828  12235.8392   12244.531  12265.5069 12268.63225 12269.68865 
##           1           1           1           1           1           1 
##   12323.936   12333.828   12347.172   12363.547  12404.8791 12430.95335 
##           1           1           1           1           1           1 
##  12475.3513 12479.70895  12485.8009 12495.29085  12523.6048  12557.6053 
##           1           1           1           1           1           1 
##   12574.049  12592.5345 12609.88702  12622.1795  12629.1656  12629.8967 
##           1           1           1           1           1           1 
##   12638.195  12643.3778   12644.589   12646.207  12648.7034  12730.9996 
##           1           1           1           1           1           1 
## 12741.16745 12797.20962 12815.44495  12829.4551 12890.05765  12913.9924 
##           1           1           1           1           1           1 
##   12925.886  12928.7911  12949.1554  12950.0712   12957.118   12979.358 
##           1           1           1           1           1           1 
##  12981.3457  12982.8747 13012.20865 13019.16105   13041.921 13047.33235 
##           1           1           1           1           1           1 
##   13063.883  13112.6048 13126.67745 13129.60345 13143.33665 13143.86485 
##           1           1           1           1           1           1 
## 13204.28565  13217.0945 13224.05705   13224.693 13228.84695  13352.0998 
##           1           1           1           1           1           1 
##   13390.559   13393.756  13405.3903  13415.0381  13429.0354   13430.265 
##           1           1           1           1           1           1 
##   13451.122  13457.9608    13462.52  13470.8044    13470.86  13555.0049 
##           1           1           1           1           1           1 
## 13607.36875  13616.3586  13635.6379 13725.47184 13747.87235  13770.0979 
##           1           1           1           1           1           1 
##   13822.803  13831.1152   13844.506  13844.7972   13880.949   13887.204 
##           1           1           1           1           1           1 
##  13887.9685  13919.8229  13937.6665 13974.45555 13981.85035  14001.1338 
##           1           1           1           1           1           1 
##  14001.2867   14007.222  14043.4767    14119.62 14133.03775 14210.53595 
##           1           1           1           1           1           1 
##   14235.072  14254.6082  14256.1928  14283.4594  14313.8463   14319.031 
##           1           1           1           1           1           1 
##  14349.8544 14358.36437 14382.70905 14394.39815  14394.5579  14410.9321 
##           1           1           1           1           1           1 
##  14418.2804 14426.07385  14449.8544 14451.83515 14455.64405   14474.675 
##           1           1           1           1           1           1 
## 14478.33015  14571.8908 14590.63205 14692.66935  14711.7438  14901.5167 
##           1           1           1           1           1           1 
##   14988.432 15006.57945 15019.76005  15161.5344   15170.069 15230.32405 
##           1           1           1           1           1           1 
##  15359.1045 15518.18025 15555.18875 15612.19335  15817.9857   15820.699 
##           1           1           1           1           1           1 
## 15828.82173 16069.08475  16085.1275  16115.3045 16138.76205   16232.847 
##           1           1           1           1           1           1 
##   16297.846 16420.49455  16450.8947 16455.70785  16577.7795 16586.49771 
##           1           1           1           1           1           1 
## 16657.71745 16776.30405 16796.41194   16884.924  17043.3414    17081.08 
##           1           1           1           1           1           1 
##  17085.2676 17128.42608  17178.6824   17179.522  17352.6803  17361.7661 
##           1           1           1           1           1           1 
##  17468.9839   17496.306 17560.37975 17626.23951  17663.1442  17748.5062 
##           1           1           1           1           1           1 
## 17878.90068 17904.52705 17929.30337   17942.106  18033.9679   18157.876 
##           1           1           1           1           1           1 
## 18218.16139  18223.4512  18246.4955   18259.216   18310.742  18328.2381 
##           1           1           1           1           1           1 
##   18608.262  18648.4217 18765.87545  18767.7377  18804.7524 18806.14547 
##           1           1           1           1           1           1 
## 18838.70366 18903.49141 18955.22017 18963.17192   18972.495    19023.26 
##           1           1           1           1           1           1 
##   19040.876  19107.7796 19144.57652   19199.944 19214.70553  19350.3689 
##           1           1           1           1           1           1 
##  19361.9988  19442.3535  19444.2658 19496.71917  19515.5416  19521.9682 
##           1           1           1           1           1           1 
##   19539.243 19594.80965 19673.33573  19719.6947 19749.38338 19798.05455 
##           1           1           1           1           1           1 
##   19933.458  19964.7463 20009.63365  20149.3229 20167.33603 20177.67113 
##           1           1           1           1           1           1 
## 20234.85475 20277.80751 20296.86345 20420.60465 20462.99766 20630.28351 
##           1           1           1           1           1           1 
## 20709.02034  20745.9891 20773.62775 20781.48892 20878.78443  20984.0936 
##           1           1           1           1           1           1 
##    21082.16 21098.55405   21195.818  21223.6758 21232.18226 21259.37795 
##           1           1           1           1           1           1 
##  21344.8467   21348.706  21472.4788 21595.38229  21659.9301 21677.28345 
##           1           1           1           1           1           1 
##  21771.3423 21774.32215  21797.0004    21880.82  21978.6769 21984.47061 
##           1           1           1           1           1           1 
##   22144.032 22192.43711  22218.1149  22331.5668 22395.74424  22412.6485 
##           1           1           1           1           1           1 
## 22462.04375     22478.6 22493.65964 23045.56616  23065.4207 23082.95533 
##           1           1           1           1           1           1 
## 23241.47453  23244.7902  23288.9284   23306.547 23401.30575 23563.01618 
##           1           1           1           1           1           1 
##   23568.272  23807.2406  23887.6627 23967.38305 24059.68019 24106.91255 
##           1           1           1           1           1           1 
##  24180.9335 24227.33724  24393.6224 24476.47851 24513.09126   24520.264 
##           1           1           1           1           1           1 
## 24535.69855 24603.04837   24667.419 24671.66334  24869.8368  24873.3849 
##           1           1           1           1           1           1 
## 24915.04626 24915.22085 25081.76784   25309.489 25333.33284   25382.297 
##           1           1           1           1           1           1 
## 25517.11363 25656.57526 25678.77845 25992.82104 26018.95052 26109.32905 
##           1           1           1           1           1           1 
## 26125.67477  26140.3603 26236.57997 26392.26029 26467.09737  26926.5144 
##           1           1           1           1           1           1 
## 27000.98473  27037.9141 27117.99378 27218.43725 27322.73386 27346.04207 
##           1           1           1           1           1           1 
## 27375.90478  27533.9129 27724.28875  27808.7251 27941.28758 28101.33305 
##           1           1           1           1           1           1 
## 28287.89766 28340.18885 28468.91901 28476.73499  28868.6639 28923.13692 
##           1           1           1           1           1           1 
##  28950.4692  29141.3603 29186.48236 29330.98315  29523.1656 30063.58055 
##           1           1           1           1           1           1 
## 30166.61817  30184.9367 30259.99556 30284.64294  30942.1918 31620.00106 
##           1           1           1           1           1           1 
## 32108.66282  32548.3405  32734.1863 32787.45859  33307.5508 33471.97189 
##           1           1           1           1           1           1 
## 33475.81715  33732.6867  33750.2918   33900.653   33907.548   34166.273 
##           1           1           1           1           1           1 
## 34254.05335  34303.1672  34439.8559   34472.841 34617.84065  34672.1472 
##           1           1           1           1           1           1 
##   34779.615  34806.4677   34828.654   34838.873 35069.37452 35147.52848 
##           1           1           1           1           1           1 
## 35160.13457    35491.64   35585.576  35595.5898  36021.0112   36085.219 
##           1           1           1           1           1           1 
##  36124.5737  36149.4835  36189.1017   36197.699 36219.40545  36307.7983 
##           1           1           1           1           1           1 
##   36397.576 36580.28216   36837.467 36898.73308 36910.60803  36950.2567 
##           1           1           1           1           1           1 
##   37079.372  37133.8982  37165.1638  37270.1512 37465.34375  37484.4493 
##           1           1           1           1           1           1 
##  37607.5277  37701.8768  37742.5757  37829.7242  38126.2465 38245.59327 
##           1           1           1           1           1           1 
##  38282.7495   38344.566   38415.474  38511.6283   38709.176       38711 
##           1           1           1           1           1           1 
##  38746.3551  38792.6856   38998.546   39047.285 39125.33225   39241.442 
##           1           1           1           1           1           1 
##  39556.4945  39597.4072  39611.7577  39722.7462 39725.51805   39727.614 
##           1           1           1           1           1           1 
##  39774.2763   39836.519  39871.7043 39983.42595 40003.33225    40103.89 
##           1           1           1           1           1           1 
##   40182.246  40273.6455  40419.0191 40720.55105  40904.1995  40932.4295 
##           1           1           1           1           1           1 
##  40941.2854  40974.1649  41034.2214 41097.16175   41661.602  41676.0811 
##           1           1           1           1           1           1 
##   41919.097  41949.2441    41999.52  42111.6647  42112.2356  42124.5153 
##           1           1           1           1           1           1 
##  42211.1382 42303.69215  42560.4304  42760.5022   42856.838  42969.8527 
##           1           1           1           1           1           1 
##  42983.4585 43254.41795  43578.9394 43753.33705  43813.8661  43896.3763 
##           1           1           1           1           1           1 
##  43921.1837  43943.8761  44202.6536  44260.7499  44400.4064   44423.803 
##           1           1           1           1           1           1 
##  44501.3982 44585.45587  44641.1974  45008.9555 45702.02235 45710.20785 
##           1           1           1           1           1           1 
##   45863.205   46113.511  46130.5265  46151.1245  46200.9851  46255.1125 
##           1           1           1           1           1           1 
##  46599.1084  46661.4424 46718.16325  46889.2612  47055.5321   47269.854 
##           1           1           1           1           1           1 
##   47291.055   47305.305    47403.88   47462.894 47496.49445 47896.79135 
##           1           1           1           1           1           1 
##    47928.03   48173.361 48517.56315 48549.17835  48673.5588  48675.5177 
##           1           1           1           1           1           1 
##    48824.45 48885.13561  48970.2476  49577.6624 51194.55914 52590.82939 
##           1           1           1           1           1           1 
## 55135.40209 58571.07448 60021.39897 62592.87309 63770.42801 
##           1           1           1           1           1
table(insurance$smoker)
## 
##   no  yes 
## 1064  274
table(insurance$sex)
## 
## female   male 
##    662    676
cor(insurance[c('age','bmi','children','charges')])
##                age       bmi   children    charges
## age      1.0000000 0.1092719 0.04246900 0.29900819
## bmi      0.1092719 1.0000000 0.01275890 0.19834097
## children 0.0424690 0.0127589 1.00000000 0.06799823
## charges  0.2990082 0.1983410 0.06799823 1.00000000
pairs(insurance[c('age','bmi','children','charges')])
library(psych)
## Warning: package 'psych' was built under R version 4.3.3
## 
## Attaching package: 'psych'
## 
## The following object is masked from 'package:kernlab':
## 
##     alpha

pairs.panels(insurance[c('age','bmi','children','charges')])

#Building model:
#m<-lm(dv~iv,data=mydata)
#Making predictions:
#p<-predict(m,test)
ins_model<-lm(charges~age+children+bmi+sex+smoker+region,data=insurance)
ins_model
## 
## Call:
## lm(formula = charges ~ age + children + bmi + sex + smoker + 
##     region, data = insurance)
## 
## Coefficients:
##     (Intercept)              age         children              bmi  
##        -11938.5            256.9            475.5            339.2  
##         sexmale        smokeryes  regionnorthwest  regionsoutheast  
##          -131.3          23848.5           -353.0          -1035.0  
## regionsouthwest  
##          -960.1
#period character(.) can be used to specify all features excluding those already specified in formula
#eg: ins_model<-lm(charges~.,data=insurance)
summary(ins_model)
## 
## Call:
## lm(formula = charges ~ age + children + bmi + sex + smoker + 
##     region, data = insurance)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -11304.9  -2848.1   -982.1   1393.9  29992.8 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -11938.5      987.8 -12.086  < 2e-16 ***
## age                256.9       11.9  21.587  < 2e-16 ***
## children           475.5      137.8   3.451 0.000577 ***
## bmi                339.2       28.6  11.860  < 2e-16 ***
## sexmale           -131.3      332.9  -0.394 0.693348    
## smokeryes        23848.5      413.1  57.723  < 2e-16 ***
## regionnorthwest   -353.0      476.3  -0.741 0.458769    
## regionsoutheast  -1035.0      478.7  -2.162 0.030782 *  
## regionsouthwest   -960.0      477.9  -2.009 0.044765 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6062 on 1329 degrees of freedom
## Multiple R-squared:  0.7509, Adjusted R-squared:  0.7494 
## F-statistic: 500.8 on 8 and 1329 DF,  p-value: < 2.2e-16
insurance$age2<-insurance$age^2
ins_model<-lm(charges~age+children+bmi+sex+smoker+region+age2,data=insurance)
ins_model
## 
## Call:
## lm(formula = charges ~ age + children + bmi + sex + smoker + 
##     region + age2, data = insurance)
## 
## Coefficients:
##     (Intercept)              age         children              bmi  
##       -6596.665          -54.575          642.024          335.211  
##         sexmale        smokeryes  regionnorthwest  regionsoutheast  
##        -138.428        23859.745         -367.812        -1031.503  
## regionsouthwest             age2  
##        -957.546            3.927
insurance$bmi30<-ifelse(insurance$bmi>=30,1,0)
ins_model<-lm(charges~age+children+bmi+sex+smoker+region+age2+bmi30,data=insurance)
ins_model
## 
## Call:
## lm(formula = charges ~ age + children + bmi + sex + smoker + 
##     region + age2 + bmi30, data = insurance)
## 
## Coefficients:
##     (Intercept)              age         children              bmi  
##       -2943.176          -28.533          630.402          153.905  
##         sexmale        smokeryes  regionnorthwest  regionsoutheast  
##        -166.295        23857.543         -400.518         -888.533  
## regionsouthwest             age2            bmi30  
##        -947.681            3.603         2727.552
ins_model2<-lm(charges~age+age2+children+bmi+sex+bmi30*smoker+region,data=insurance)
ins_model2
## 
## Call:
## lm(formula = charges ~ age + age2 + children + bmi + sex + bmi30 * 
##     smoker + region, data = insurance)
## 
## Coefficients:
##     (Intercept)              age             age2         children  
##         134.251          -32.685            3.732          678.561  
##             bmi          sexmale            bmi30        smokeryes  
##         120.020         -496.824        -1000.140        13404.687  
## regionnorthwest  regionsoutheast  regionsouthwest  bmi30:smokeryes  
##        -279.204         -828.547        -1222.644        19810.753
insurance$pred<-predict(ins_model2,insurance)
cor(insurance$pred,insurance$charges)
## [1] 0.9308031
plot(insurance$pred,insurance$charges)

#abline(a=0,b=1,col="red",lwd=3,lty=2)
predict(ins_model2,data.frame(age=30,age2=30^2,children=2,bmi=30,sex='male',bmi30=1,smoker='no',region='northeast'))
##        1 
## 5972.859
predict(ins_model2,data.frame(age=30,age2=30^2,children=2,bmi=30,sex='female',bmi30=1,smoker='no',region='northeast'))
##        1 
## 6469.683
predict(ins_model2,data.frame(age=30,age2=30^2,children=0,bmi=30,sex='male',bmi30=1,smoker='no',region='northeast'))
##        1 
## 4615.736
6469.683-4615.736
## [1] 1853.947