Lesson 4 ======================================================== *** ### Scatterplots and Perceived Audience Size Notes: *** ### Scatterplots Notes: ```{r Scatterplots} library(ggplot2) pf <- read.csv('pseudo_facebook.tsv', sep = '\t') ggplot(aes(x = age, y = friend_count), data = pf) + geom_point() ``` *** #### What are some things that you notice right away? Response: All of the data points are grouped into vertical lines and that the younger the age the more likely they are to have more friends. ### ggplot Syntax Notes: ```{r ggplot Syntax} ggplot(aes(x = age, y = friend_count), data = pf) + geom_point() + xlim(13, 90) summary(pf$age) ``` Build one layer at a time to find errors easier ### Overplotting Notes: ```{r Overplotting} ggplot(aes(x = age, y = friend_count), data = pf) + geom_jitter(alpha = 1/20) + xlim(13, 90) ``` #### What do you notice in the plot? Response: The bar for 69 is still clearly visible and it is more obvious that the number generally decreases as the age increases. ### Coord_trans() Notes: ```{r Coord_trans()} ``` #### Look up the documentation for coord_trans() and add a layer to the plot that transforms friend_count using the square root function. Create your plot! ```{r} ggplot(aes(x = age, y = friend_count), data = pf) + geom_point(alpha = 1/20) + xlim(13, 90) + coord_trans(y = "sqrt") ``` #### What do you notice? First off coord_trans does not work with geom_jitter, second the datapoints near the bottom are more spread out vertically to present them as more of a focus. To use jitter you need more advanced syntax to only jitter the ages, also to prevent possible negatives if 0 is jittered. To do this in `geom_point()` pass `position = position_jitter(h = 0)` ```{r coord_trans_advanced} ggplot(aes(x = age, y = friend_count), data = pf) + geom_point(alpha = 1/20, position = position_jitter(h = 0)) + xlim(13, 90) + coord_trans(y = "sqrt") ``` ### Alpha and Jitter Notes: ```{r Alpha and Jitter} ggplot(aes(x = age, y = friendships_initiated, color = gender), data = pf) + geom_point(alpha = 1/10, position = position_jitter(h = 0)) + xlim(13, 90) + coord_trans(y = "sqrt") ``` *** ### Overplotting and Domain Knowledge Notes: plotting as a percentage of the whole ### Conditional Means Notes: ```{r Conditional Means} library(dplyr) age_groups <- group_by(pf, age) pf.fc_by_age <- summarise(age_groups, friend_count_mean = mean(friend_count), friend_count_median = median(friend_count), n = n()) pf.fc_by_age <- arrange(pf.fc_by_age, age) ggplot(aes(x = age, y = friend_count_mean), data = pf.fc_by_age) + geom_line() + xlim(13,90) ``` ### Overlaying Summaries with Raw Data Notes: ```{r Overlaying Summaries with Raw Data} ggplot(aes(x = age, y = friendships_initiated), data = pf) + geom_point(alpha = 1/10, position = position_jitter(h = 0), color = 'orange') + xlim(13, 90) + coord_trans(y = "sqrt") + geom_line(stat = 'summary', fun.y = mean) + geom_line(stat = 'summary', fun.y = median, color = 'blue') + geom_line(stat = 'summary', fun.y = quantile, fun.args = list(probs = 0.1), color = 'red', linetype = 2) + geom_line(stat = 'summary', fun.y = quantile, fun.args = list(probs = 0.9), color = 'red', linetype = 2) + coord_cartesian(xlim = c(13,70), ylim = c(0,1000)) ``` #### What are some of your observations of the plot? Response: I notice that the median is always lower than the mean and that the median is closer to the center of the main body of datapoints. It appears that the data is long tailed towards the high friend counts which pulls the mean upwards. ### Moira: Histogram Summary and Scatterplot See the Instructor Notes of this video to download Moira's paper on perceived audience size and to see the final plot. Notes: *** ### Correlation Notes: ```{r Correlation} ``` Look up the documentation for the cor.test function. What's the correlation between age and friend count? Round to three decimal places. Response: *** ### Correlation on Subsets Notes: ```{r Correlation on Subsets} with( , cor.test(age, friend_count)) ``` *** ### Correlation Methods Notes: *** ## Create Scatterplots Notes: ```{r} ``` *** ### Strong Correlations Notes: ```{r Strong Correlations} ``` What's the correlation betwen the two variables? Include the top 5% of values for the variable in the calculation and round to 3 decimal places. ```{r Correlation Calcuation} ``` Response: *** ### Moira on Correlation Notes: *** ### More Caution with Correlation Notes: ```{r More Caution With Correlation} install.packages('alr3') library(alr3) ``` Create your plot! ```{r Temp vs Month} ``` *** ### Noisy Scatterplots a. Take a guess for the correlation coefficient for the scatterplot. b. What is the actual correlation of the two variables? (Round to the thousandths place) ```{r Noisy Scatterplots} ``` *** ### Making Sense of Data Notes: ```{r Making Sense of Data} ``` *** ### A New Perspective What do you notice? Response: Watch the solution video and check out the Instructor Notes! Notes: *** ### Understanding Noise: Age to Age Months Notes: ```{r Understanding Noise: Age to Age Months} ``` *** ### Age with Months Means ```{r Age with Months Means} ``` Programming Assignment ```{r Programming Assignment} ``` *** ### Noise in Conditional Means ```{r Noise in Conditional Means} ``` *** ### Smoothing Conditional Means Notes: ```{r Smoothing Conditional Means} ``` *** ### Which Plot to Choose? Notes: *** ### Analyzing Two Variables Reflection: *** Click **KnitHTML** to see all of your hard work and to have an html page of this lesson, your answers, and your notes!