Outline Research Question: What determines height? Data Input Look at One Variable Compare Two Variables Children’s Height and Parents Height Children’s Height and Gender Graphic Packages: ggplot2
What factors are most responsible for height? Outcome = (Model) + Error
Galton’s Notebook on Families & Height
X1X2X3Y Galton’s Family Height Dataset
> getwd() [1] "C:/Users/johnp_000/Documents" > setwd()
Dataset Input Function Filename Object h <- read.csv("GaltonFamilies.csv")
str() summary() Data Types: Numbers and Factors/Categorical
Outline One Variable: Univariate Dependent / Outcome Variable Two Variables: Bivariate Outcome and each Predictor All Four Variables: Multivariate
Steps Continuous Categorical Histogram Scatter Boxplot Child’s Height Linear Regression Dad’s Height Gender Continuous Y X1, X2 X3 Type Variable Mom’s Height
Frequency Distribution, Histogram hist(h$child)
Area = 1 Density Plot plot(density(h$childHeight))
hist(h$childHeight,freq=F, breaks =25, ylim = c(0,0.14)) curve(dnorm(x, mean=mean(h$childHeight), sd=sd(h$childHeight)), col="red", add=T) Mode, Bimodal
Grammar of Graphics formations Legend Axes Seven Components ggplot2 built using the grammar of graphics approach
Asst. Professor of Statistics at Rice University ggplot2 plyr reshape rggobi profr Hadley Wickman and ggplot2
In ggplot2 a plot is made up of layers. ggplot2 Plot Grammar of Graphics Layer -Data - Mapping -Geom -Stat -Postiion Scale Coord Facet
ggplot2 library(ggplot2) h.gg <- ggplot(h, aes(child)) h.gg + geom_histogram(binwidth = 1 ) + labs(x = "Height", y = "Frequency") h.gg + geom_density()
ggplot2 h.gg <- ggplot(h, aes(child)) + theme(legend.position = "right") h.gg + geom_density() + labs(x = "Height", y = "Frequency") h.gg + geom_density(aes(fill=factor(gender)), size=2)
Steps Continuous Categorical Histogram Scatter Boxplot Child’s Height Linear Regression Dad’s Height Gender Continuous Y X1, X2 X3 Type Variable Mom’s Height
Correlation and Regression
1.Calculate the difference between the mean and each person’s score for the first variable (x). 2.Calculate the difference between the mean and their value for the second variable (y). 3.Multiply these “error” values. 4.Add these values to get the cross product deviations. 5.The covariance is the average of cross-product deviations Covariance
Y X Persons 2,3, and 5 look to have similar magnitudes from their means
Covariance Calculate the error [deviation] between the mean and each subject’s score for the first variable (x). Calculate the error [deviation] between the mean and their score for the second variable (y). Multiply these error values. Add these values and you get the cross product deviations. The covariance is the average cross-product deviations:
Covariance depends upon the units of measurement Normalize the data Divide by the standard deviations of both variables. The standardized version of covariance is known as the correlation coefficient Standardizing the Covariance
Correlation ?cor cor(h$father, h$child)
Scatterplot Matrix: pairs()
Correlations Matrix library(car) scatterplotMatrix(heights)
ggplot2
Steps Continuous Categorical Histogram Scatter Boxplot Child’s Height Linear Regression Dad’s Height Gender Continuous Y X1, X2 X3 Type Variable Mom’s Height
Box Plot
Children’s Height vs. Gender boxplot(h$child~gender,data=h, col=(c("pink","lightblue")), main="Children's Height by Gender", xlab="Gender", ylab="")
Descriptive Stats: Box Plot ======
Subset Males men<- subset(h, gender=='male')
Subset Females women <- subset(h, gender==‘female')
Children’s Height: Males hist(men$childHeight)
Children’s Height: Females hist(women$child)
ggplot2 library(ggplot2) h.bb <- ggplot(h, aes(factor(gender), child)) h.bb + geom_boxplot() h.bb + geom_boxplot(aes(fill = factor(gender)))
Steps Continuous Categorical Histogram Scatter Boxplot Child’s Height Dad’s Height Gender Continuous Type Variable Mom’s Height