This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(e1071) # it includes function to compute skewness | |
library(plyr) # it allows to wrangle data | |
library(ggplot2) # it allows to create a number of different types of plots | |
ais2 <- subset(ais, sex=="m") # only male athletes | |
ais3 = ais2[,c(3,4)] # subset column number that correspond to "hg" and "hc" | |
newdata <- rename(ais3, c("hg"="HEMAGLOBIN", "hc"="HEMATOCRIT")) | |
str(newdata) | |
summary(newdata) # overview of the two selected variable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
qplot(HEMAGLOBIN, HEMATOCRIT, data = newdata, | |
main = "HEMAGLOBIN and HEMATOCRIT relationship") + | |
theme(plot.title = element_text(hjust = 0.5)) + | |
geom_point(colour = "blue", size = 1.5) + | |
scale_y_continuous(breaks = c(30:65), minor_breaks = NULL) + | |
scale_x_continuous(breaks = c(10:25), minor_breaks = NULL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
par(mfrow=c(1, 2)) # it divides graph area in two parts | |
boxplot(newdata$HEMAGLOBIN, col = "yellow", border="blue", | |
main = "HEMAGLOBIN boxplot", | |
ylab = "g per decaliter") | |
boxplot(newdata$HEMATOCRIT, col = "orange", border="blue", | |
main = "HEMATROCRIT boxplot", | |
ylab = "percent values") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Histogram of HEMAGLOBIN | |
qplot(HEMAGLOBIN, data = newdata, geom="histogram", binwidth=0.5, | |
fill=I("azure4"), col=I("azure3")) + | |
labs(title = "HEMAGLOBIN") + | |
theme(plot.title = element_text(hjust = 0.5)) + | |
labs(x ="Concentration (in g per decaliter)") + | |
labs(y = "Frequency") + | |
scale_y_continuous(breaks = c(0,5,10,15,20,25,30,35,40,45,50), minor_breaks = NULL) + | |
scale_x_continuous(breaks = c(10:25), minor_breaks = NULL) + | |
geom_vline(xintercept = mean(newdata$HEMAGLOBIN), show_guide=TRUE, color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Histogram of HEMATOCRIT | |
qplot(HEMATOCRIT, data = newdata, geom="histogram", binwidth=1, | |
fill=I("azure4"), col=I("azure3")) + | |
labs(title = "HEMATOCRIT") + | |
theme(plot.title = element_text(hjust = 0.5)) + | |
labs(x ="percent values") + | |
labs(y = "Frequency") + | |
scale_y_continuous(breaks = c(0,5,10,15,20,25), minor_breaks = NULL) + | |
scale_x_continuous(breaks = c(30:65), minor_breaks = NULL) + | |
geom_vline(xintercept = mean(newdata$HEMATOCRIT), show_guide=TRUE, color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
par(mfrow=c(1, 2)) # it divides graph area in two parts | |
plot(density(newdata$HEMAGLOBIN), main="Density: HEMAGLOBIN", ylab="Frequency", | |
sub=paste("Skewness:", round(e1071::skewness(newdata$HEMAGLOBIN), 2))) | |
polygon(density(newdata$HEMAGLOBIN), col="yellow") | |
plot(density(newdata$HEMATOCRIT), main="Density: HEMATOCRIT", ylab="Frequency", | |
sub=paste("Skewness:", round(e1071::skewness(newdata$HEMATOCRIT), 2))) | |
polygon(density(newdata$HEMATOCRIT), col="orange") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Show the relationship creating a regression line | |
qplot(HEMAGLOBIN, HEMATOCRIT, data = newdata, | |
main = "HEMAGLOBIN and HEMATOCRIT relationship") + | |
theme(plot.title = element_text(hjust = 0.5)) + | |
stat_smooth(method="lm", col="red", size=1) + | |
geom_point(colour = "blue", size = 1.5) + | |
scale_y_continuous(breaks = c(30:65), minor_breaks = NULL) + | |
scale_x_continuous(breaks = c(10:25), minor_breaks = NULL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(123) # setting seed to reproduce results of random sampling | |
HEMAGLOBIN_CENT = scale(newdata$HEMAGLOBIN, center=TRUE, scale=FALSE) # center the variable | |
# Show the relationship with new variable centered, creating a regression line | |
qplot(HEMAGLOBIN_CENT, HEMATOCRIT, data = newdata, | |
main = "HEMAGLOBIN_CENT and HEMATOCRIT relationship") + | |
theme(plot.title = element_text(hjust = 0.5)) + | |
stat_smooth(method="lm", col="red", size=1) + | |
geom_point(colour = "blue", size = 1.5) + | |
scale_y_continuous(breaks = c(30:65), minor_breaks = NULL) + | |
scale_x_continuous(breaks = c(-2,-1.5,-1,-0.5,0,0.5,1,1.5,2,2.5,3,3.5,4), minor_breaks = NULL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod1 = lm(HEMATOCRIT ~ HEMAGLOBIN_CENT, data = newdata) | |
summary(mod1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
modSummary <- summary(mod1) # capture model summary as an object | |
modCoeff <- modSummary$coefficients # model coefficients | |
beta.estimate <- modCoeff["HEMAGLOBIN_CENT", "Estimate"] # get beta coefficient estimate | |
std.error <- modCoeff["HEMAGLOBIN_CENT", "Std. Error"] # get standard error | |
t_value <- beta.estimate/std.error # calculate t statistic | |
print(t_value) # print t-value |
OlderNewer