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:
lung <- read.csv("LungCapData.csv")
head(lung)
## LungCap Age Height Smoke Gender Caesarean
## 1 6.475 6 62.1 no male no
## 2 10.125 18 74.7 yes female no
## 3 9.550 16 69.7 no female yes
## 4 11.125 14 71.0 no male no
## 5 4.800 5 56.9 no male no
## 6 6.225 11 58.7 no female no
library(ggplot2)
ggplot(lung, aes(x = LungCap)) + geom_histogram(color = 'blue')
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(lung, aes(x = LungCap, y = Age)) + geom_boxplot()
## Warning: Continuous x aesthetic -- did you forget aes(group=...)?
ggplot(lung, aes(x = Gender)) + geom_bar()
prop.table(table(lung$Gender))
##
## female male
## 0.4937931 0.5062069
ggplot(lung, aes(x = Gender, y = LungCap)) + geom_boxplot()
ggplot(lung, aes(x = Gender, fill = factor(Smoke))) + geom_bar(position = position_dodge(1))
linearmodel <- lm(data = lung)
summary(linearmodel)
##
## Call:
## lm(data = lung)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.3388 -0.7200 0.0444 0.7093 3.0172
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -11.32249 0.47097 -24.041 < 2e-16 ***
## Age 0.16053 0.01801 8.915 < 2e-16 ***
## Height 0.26411 0.01006 26.248 < 2e-16 ***
## Smokeyes -0.60956 0.12598 -4.839 1.60e-06 ***
## Gendermale 0.38701 0.07966 4.858 1.45e-06 ***
## Caesareanyes -0.21422 0.09074 -2.361 0.0185 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.02 on 719 degrees of freedom
## Multiple R-squared: 0.8542, Adjusted R-squared: 0.8532
## F-statistic: 842.8 on 5 and 719 DF, p-value: < 2.2e-16
par(mfrow = c(2,2))
plot(linearmodel)
library(caret)
## Loading required package: lattice
library(lattice)
reducedlinearmodel <- lm(LungCap ~ Age + Height + Smoke + Gender, data = lung)
predictions <- predict(reducedlinearmodel, lung[-1])
RMSE(predictions, lung$LungCap)
## [1] 1.019516
s <- summary(reducedlinearmodel)
s
##
## Call:
## lm(formula = LungCap ~ Age + Height + Smoke + Gender, data = lung)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.2915 -0.7360 0.0184 0.7125 3.0599
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -11.33282 0.47245 -23.987 < 2e-16 ***
## Age 0.16012 0.01806 8.864 < 2e-16 ***
## Height 0.26363 0.01009 26.123 < 2e-16 ***
## Smokeyes -0.61774 0.12633 -4.890 1.24e-06 ***
## Gendermale 0.38528 0.07991 4.822 1.74e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.023 on 720 degrees of freedom
## Multiple R-squared: 0.8531, Adjusted R-squared: 0.8523
## F-statistic: 1045 on 4 and 720 DF, p-value: < 2.2e-16
#Null <- There is no relationship between two phenomena.
#alternative <- The Difference beween the two models is significant.
ano <- anova(linearmodel,reducedlinearmodel, test = "F")
summary(ano)
## Res.Df RSS Df Sum of Sq
## Min. :719.0 Min. :747.8 Min. :-1 Min. :-5.797
## 1st Qu.:719.2 1st Qu.:749.2 1st Qu.:-1 1st Qu.:-5.797
## Median :719.5 Median :750.7 Median :-1 Median :-5.797
## Mean :719.5 Mean :750.7 Mean :-1 Mean :-5.797
## 3rd Qu.:719.8 3rd Qu.:752.1 3rd Qu.:-1 3rd Qu.:-5.797
## Max. :720.0 Max. :753.6 Max. :-1 Max. :-5.797
## NA's :1 NA's :1
## F Pr(>F)
## Min. :5.574 Min. :0.0185
## 1st Qu.:5.574 1st Qu.:0.0185
## Median :5.574 Median :0.0185
## Mean :5.574 Mean :0.0185
## 3rd Qu.:5.574 3rd Qu.:0.0185
## Max. :5.574 Max. :0.0185
## NA's :1 NA's :1
ano
## Analysis of Variance Table
##
## Model 1: LungCap ~ Age + Height + Smoke + Gender + Caesarean
## Model 2: LungCap ~ Age + Height + Smoke + Gender
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 719 747.78
## 2 720 753.57 -1 -5.7968 5.5737 0.0185 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1