#Exercise 1 using median as data is slightly left skewed
library(ggplot2)
ggplot(data=LifeExp, aes(x=Schooling))+
  geom_histogram(bins=10, col='white', fill='blue', alpha=(.7))
## Warning: Removed 163 rows containing non-finite outside the scale range
## (`stat_bin()`).

median(Schooling, na.rm = TRUE)
## [1] 12.3
IQR(Schooling, na.rm=TRUE, type=7)
## [1] 4.2
#Exercise 2 Boxplot with 5 number summary plotted as red points
boxplot.default(Schooling, horizontal = TRUE, xlab= "Years in School", main = "Schooling Boxplot", col='green')
FNSS=fivenum(Schooling, na.rm = TRUE)
points(FNSS,rep(1,5),col='red')

FNSS # The box plot aligns with my five number summary with the minimum as an outlier at 0, lower quartile=10.1, median=12.3, upper quartile=14.3, and maximum as an outlier at 20.7.
## [1]  0.0 10.1 12.3 14.3 20.7
#Exercise 3
tb_Status=table(Status)
barplot(tb_Status, ylim=c(0,2500))

#Exercise 4
plot(x=Alcohol,y=Life.expectancy)

cor(Alcohol,Life.expectancy, use="complete.obs")
## [1] 0.4048768
# Moderate positive linear correlation (r ≈ 0.405), with considerable scatter. The correlation can summarize the overall linear tendency.
#Exercise 5
plot(GDP,Life.expectancy)

cor(GDP,Life.expectancy, use="complete.obs")
## [1] 0.4614552
# Moderate positive linear correlation (r ≈ .461), large scatter at low gdp leveling off as GDP increases. The curved pattern limits how well correlation describes the relationship.
#Exercise 6
boxplot(GDP~Status, horizontal = T)

#Developed countries have higher median, larger range and IQR, and tend to have larger GDP per capita. Both have high value outlier's. Both Right skewed.
#Exercise 7
tbl1=table(department, admitted)
tbl1
##           admitted
## department Admitted Rejected
##          A      601      332
##          B      370      215
##          C      322      596
##          D      269      523
##          E      147      437
##          F       46      668
#Exercise 8
tbl2=prop.table(tbl1, margin=1)
tbl2
##           admitted
## department   Admitted   Rejected
##          A 0.64415863 0.35584137
##          B 0.63247863 0.36752137
##          C 0.35076253 0.64923747
##          D 0.33964646 0.66035354
##          E 0.25171233 0.74828767
##          F 0.06442577 0.93557423
#Department A has the highest admission rate
#Exercise 9
ggplot(Berkeley, aes(department,fill=admitted))+
  geom_bar(position="fill")+ ylab("proportion")