R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(psych)
## Warning: package 'psych' was built under R version 4.4.3
insurance <- read.csv("C:/Users/jackson4705/Downloads/insurance.csv", stringsAsFactors=TRUE)
   View(insurance)
hist(insurance$charges)

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

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

ins_model <- lm(charges ~ age + children + bmi + sex + smoker + region, data = insurance)
ins_model <- lm(charges ~ ., data = insurance)
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
insurance$age2 <- insurance$age^2
insurance$bmi30 <- ifelse(insurance$bmi >= 30, 1, 0)

ins_model2 <- lm(charges ~ age + age2 + children + bmi + sex + bmi30*smoker + region, data = insurance)

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 = 'female', bmi30 = 1, smoker = 'no', region = 'northeast'))
##        1 
## 5112.561

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.