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:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

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.

insurance <- read.csv("C:/Users/Home/Downloads/insurance.csv", stringsAsFactors=TRUE)
# Set the CRAN mirror
options(repos = "https://cloud.r-project.org/")

# Install the "psych" package
install.packages("psych")
## Installing package into 'C:/Users/Home/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'psych' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Home\AppData\Local\Temp\RtmpovLYEV\downloaded_packages
library(psych)
## Warning: package 'psych' was built under R version 4.3.3
View(insurance)
#in this data set we are trying see the relation between the attribute
str(insurance)# to see the structure of the data set
## '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) # note that mean and median is quite far ,this means 
##       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
 #from each other

hist(insurance$charges)

table(insurance$region)
## 
## northeast northwest southeast southwest 
##       324       325       364       325
prop.table(table(insurance$region))
## 
## northeast northwest southeast southwest 
## 0.2421525 0.2428999 0.2720478 0.2428999
table(insurance$sex)
## 
## female   male 
##    662    676
prop.table(table(insurance$sex))
## 
##    female      male 
## 0.4947683 0.5052317
table(insurance$smoker)
## 
##   no  yes 
## 1064  274
prop.table(table(insurance$smoker))
## 
##        no       yes 
## 0.7952167 0.2047833
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)
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
insurance$bmi30<-ifelse(insurance$bmi>=30,1,0)
insurance$bmi30
##    [1] 0 1 1 0 0 0 1 0 0 0 0 0 1 1 1 0 1 0 1 1 1 1 1 1 0 0 0 1 0 1 1 0 0 0 1 0 1
##   [38] 0 1 1 0 1 0 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1
##   [75] 0 1 0 1 1 1 0 1 1 1 1 0 1 0 0 0 1 0 0 1 1 1 1 1 0 0 1 0 1 0 0 0 0 1 0 1 1
##  [112] 0 1 1 1 0 1 0 0 0 1 0 0 1 1 0 0 1 0 1 0 0 1 0 0 0 1 0 1 1 0 1 0 0 0 1 1 1
##  [149] 1 0 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 0 1 1 1 0 1 1 0 1 1 1 0 0 0 1 0 1 0 0 1
##  [186] 1 0 1 1 1 1 0 0 0 1 1 1 0 0 1 1 1 0 1 0 0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1
##  [223] 1 1 0 1 1 1 1 0 1 0 0 0 0 0 0 1 0 1 1 0 0 1 0 1 1 1 0 0 0 1 1 1 1 0 1 1 0
##  [260] 1 0 0 0 1 1 1 0 1 1 0 0 1 1 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 1 0 0 0
##  [297] 0 0 1 0 0 0 1 1 1 1 0 1 1 1 0 0 1 1 1 1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