1.Obesity data analysis

Read input data into R

obdata = read.csv("obesity data.csv")
head(obdata)
##   id gender height weight  bmi age WBBMC wbbmd   fat  lean pcfat
## 1  1      F    150     49 21.8  53  1312  0.88 17802 28600  37.3
## 2  2      M    165     52 19.1  65  1309  0.84  8381 40229  16.8
## 3  3      F    157     57 23.1  64  1230  0.84 19221 36057  34.0
## 4  4      F    156     53 21.8  56  1171  0.80 17472 33094  33.8
## 5  5      M    160     51 19.9  54  1681  0.98  7336 40621  14.8
## 6  6      F    153     47 20.1  52  1358  0.91 14904 30068  32.2

Linear regression analysis

summary(lm(pcfat ~ bmi, data = obdata))
## 
## Call:
## lm(formula = pcfat ~ bmi, data = obdata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -19.612  -4.181   1.392   4.690  18.241 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.39889    1.36777   6.141 1.11e-09 ***
## bmi          1.03619    0.06051  17.123  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.45 on 1215 degrees of freedom
## Multiple R-squared:  0.1944, Adjusted R-squared:  0.1937 
## F-statistic: 293.2 on 1 and 1215 DF,  p-value: < 2.2e-16

Summary

The mean of percent body fat is 31.6047859.

Plot the correlation between BMI and pcfat

library(ggplot2)
p = ggplot(data=obdata, aes(x=bmi, y=pcfat, ol=gender))
p + geom_point() + geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'

2. R Multiple Plots

An example

par(mfrow=c(2,2))
N <- 200
x <- runif(N, -4, 4)
y <- sin(x) + 0.5*rnorm(N)

plot(x,y, main = "Scatter plot of y and x")
hist(x, main = " Histogram of x ")
boxplot(y, main = "Box plot of y")
barplot(x, main = "Bar chart of x")

par(mfrow=c(1,1))

3. Chart Analysis

Basically, charts can be categorized into two main types: describe one variable, and describe relations between two or more variables.

chartdata = read.table("chol.txt", header = TRUE, na.strings = ".")

3.1 One discrete variable: barplot

sex.freq <- table(chartdata$sex)
sex.freq
## 
## Nam  Nu 
##  22  28
barplot(sex.freq, main = "Frequency of males and females")

3.2 Two discrete variable: barplot

We can separate a continuous variable to multiple discrete groups. Consequently, we can calculate the frequency of new variable generating from the previous step.

ageg <- cut(chartdata$age, 3)
table(ageg)
## ageg
##   (42,54.7] (54.7,67.3]   (67.3,80] 
##          19          24           7
age.sex <- table(chartdata$sex, ageg)
age.sex
##      ageg
##       (42,54.7] (54.7,67.3] (67.3,80]
##   Nam        10          10         2
##   Nu          9          14         5
par(mfrow=c(1,2))
barplot(age.sex, main = "Males and females")
barplot(age.sex, main = "Males and females in each group", beside = TRUE, xlab="Age group")

par(mfrow=c(1,1))