The following command fits a linear regression model relating the six independent variables to the total medical expenses.
## Training a model on the data ----
ins_model <- lm(expenses ~ age + children + bmi +
sex + smoker + region,
data = insurance)
## Training a model on the data ----
ins_model <- lm(expenses ~ ., data = insurance)
# this is equivalent to above
# see the estimated beta coefficients
ins_model
##
## Call:
## lm(formula = expenses ~ ., data = insurance)
##
## Coefficients:
## (Intercept) age sexmale bmi
## -11941.6 256.8 -131.4 339.3
## children smokeryes regionnorthwest regionsoutheast
## 475.7 23847.5 -352.8 -1035.6
## regionsouthwest
## -959.3
Also, lm() function automatically applied a technique known as dummy coding to each of the factor-type variables we included in the model.