# Linear regression in R
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
AgeHeight <- read_excel("AgeHeight.xlsx")
ageandheight <- read_excel("AgeHeight.xlsx")
# Upload data
lmHeight = lm(height~ age, data = ageandheight)
# Create the linear regression
summary(lmHeight)
## 
## Call:
## lm(formula = height ~ age, data = ageandheight)
## 
## 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
# Create a linear regression w/ 2 variables
lmHeight2 = lm(height~ age + no_siblings, data = ageandheight)
summary(lmHeight2)
## 
## Call:
## lm(formula = height ~ age + no_siblings, data = ageandheight)
## 
## 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
# Insurance companies business model
insurance <- read.csv("C:/Users/angel/Documents/RPractice/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
# Histogram of charges
hist(insurance$charges)

# Proportion of sex and smokers
table(insurance$sex)
## 
## female   male 
##    662    676
table(insurance$smoker)
## 
##   no  yes 
## 1064  274
# **Relationship among features the correlation matrix**
# cor(x,y) > 0.5 <- Strong correlation
# 0.2 < cor(x,y) < 0.5 <- OK correlation
# cor(x,y) < 0.2 <- No correlation -> x & y are truly independent
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
# **Visualizing relationships among features - scatter plot**
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 objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
pairs.panels(insurance[c('age','bmi', 'children','charges')])

# Training a model on the data
#ins_model <- lm(charges~ age + children + bmi + sex + smoker + region, data = insurance)
ins_model <- lm(charges~ ., data = insurance)
print(ins_model)
## 
## Call:
## lm(formula = charges ~ ., data = insurance)
## 
## Coefficients:
##     (Intercept)              age          sexmale              bmi  
##        -11938.5            256.9           -131.3            339.2  
##        children        smokeryes  regionnorthwest  regionsoutheast  
##           475.5          23848.5           -353.0          -1035.0  
## regionsouthwest  
##          -960.1
summary(ins_model)
## 
## Call:
## lm(formula = charges ~ ., 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 ***
## sexmale           -131.3      332.9  -0.394 0.693348    
## bmi                339.2       28.6  11.860  < 2e-16 ***
## children           475.5      137.8   3.451 0.000577 ***
## 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
# Non-linear model
insurance$age2 <-insurance$age^2
# Transform -> converting a numeric variable to a binary indicator
insurance$bmi30 <- ifelse(insurance$bmi >= 30, 1, 0)
# Adding interaction effects
# shorthand -> charges~ bmi30 * smoker
# charges~ bmi30 + smokeryes + bmi30:smokeryes <- individual + individual + combined intersection
ins_model2 <- lm(charges~ age + age2 + children + bmi + bmi30 + smoker + region, data = insurance)
print(ins_model2)
## 
## Call:
## lm(formula = charges ~ age + age2 + children + bmi + bmi30 + 
##     smoker + region, data = insurance)
## 
## Coefficients:
##     (Intercept)              age             age2         children  
##       -3018.810          -28.208            3.601          629.121  
##             bmi            bmi30        smokeryes  regionnorthwest  
##         153.553         2722.826        23842.050         -399.461  
## regionsoutheast  regionsouthwest  
##        -887.945         -946.844
summary(ins_model2)
## 
## Call:
## lm(formula = charges ~ age + age2 + children + bmi + bmi30 + 
##     smoker + region, data = insurance)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12565.9  -3385.9     56.7   1342.0  29250.8 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -3018.810   1821.496  -1.657 0.097690 .  
## age               -28.208     80.419  -0.351 0.725826    
## age2                3.601      1.003   3.589 0.000344 ***
## children          629.121    142.304   4.421 1.06e-05 ***
## bmi               153.553     46.038   3.335 0.000875 ***
## bmi30            2722.826    547.385   4.974 7.41e-07 ***
## smokeryes       23842.050    406.089  58.711  < 2e-16 ***
## regionnorthwest  -399.461    469.502  -0.851 0.395023    
## regionsoutheast  -887.945    472.699  -1.878 0.060537 .  
## regionsouthwest  -946.844    471.081  -2.010 0.044640 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5975 on 1328 degrees of freedom
## Multiple R-squared:  0.7582, Adjusted R-squared:  0.7565 
## F-statistic: 462.7 on 9 and 1328 DF,  p-value: < 2.2e-16