This is assignment 1. Answer the following questions below. You must turn in this file as an .html file (uploaded to Canvas) by the assignment deadline. Please note the following:
eval = FALSE in the code chunk
options.This section is designed to build familiarity with the data we’re going to use this semester.
I selected the variable gdppc (GDP per capita) from the world dataset. This variable is continuous and typically shows a right-skewed distribution because a small number of very high-income countries pull the upper tail upward. Interpreting gdppc is most informative when comparing across regions or when using transformations (like a natural log) to reduce skew.
When I summarize gdppc in the world dataset, the values
range from a minimum of 683.8 to a maximum of 123,593. The median is
9,863.2, while the mean is higher at 16,326.8. This difference shows the
distribution is skewed to the right because a few very wealthy countries
increase the average substantially. The first quartile is 3,249.1 and
the third quartile is 21,183.4, so most countries fall in that middle
range. There are 6 missing values.
# Overall summary of gdppc
world %>%
summarise(
min_gdppc = min(gdppc, na.rm = TRUE),
q1_gdppc = quantile(gdppc, 0.25, na.rm = TRUE),
median_gdppc = median(gdppc, na.rm = TRUE),
mean_gdppc = mean(gdppc, na.rm = TRUE),
q3_gdppc = quantile(gdppc, 0.75, na.rm = TRUE),
max_gdppc = max(gdppc, na.rm = TRUE),
n_missing = sum(is.na(gdppc))
)
## min_gdppc q1_gdppc median_gdppc mean_gdppc q3_gdppc max_gdppc n_missing
## 1 683.7921 3249.066 9863.205 16326.78 21183.4 123593 6
# Means and standard deviations of GDP per capita by region
world %>%
group_by(region) %>%
summarize(
mean_gdppc = mean(gdppc, na.rm = TRUE),
sd_gdppc = sd(gdppc, na.rm = TRUE),
n = sum(!is.na(gdppc))
) %>%
arrange(desc(mean_gdppc))
## # A tibble: 7 × 4
## region mean_gdppc sd_gdppc n
## <fct> <dbl> <dbl> <int>
## 1 North America 44201. 5902. 2
## 2 ME and North Africa 29875. 29695. 19
## 3 Europe 26057. 16988. 45
## 4 East Asia 15695. 20508. 26
## 5 Latin America 12131. 6250. 32
## 6 South Asia 4945. 3160. 8
## 7 Sub-Saharan Africa 4752. 7479. 44
The overall summary of gdppc reports the minimum, quartiles, median, mean, maximum, and number of missing values. The mean exceeds the median, indicating right-skew. A second table reports the mean and standard deviation of gdppc by region, which shows the expected development patterns and within-region variability.
I used a boxplot to visualize GDP per capita. This method is appropriate because it clearly shows the median, the spread of most values, and the presence of outliers. The thick horizontal line inside the box represents the median, the box covers the interquartile range, and the whiskers extend to the rest of the data. The dots above the whiskers indicate outliers. In this case, the plot shows that most countries are below about 20,000, while several very high values appear as outliers, which is consistent with the skewed distribution.
ggplot(world, aes(x = region, y = gdppc)) +
geom_boxplot(outlier.alpha = 0.6) +
coord_flip() +
labs(title = "GDP per Capita by Region",
x = "Region",
y = "GDP per Capita") +
theme_minimal(base_size = 12)
As an alternative visualization, I used a histogram of
gdppc. This plot shows how many countries fall into each
income range. It makes the right-skew very clear because the bars are
tall at lower values and then thin out as incomes rise. Unlike the
boxplot, which summarizes medians and quartiles by group, the histogram
displays the full distribution across all countries and highlights the
long right tail.
ggplot(world, aes(x = gdppc)) +
geom_histogram(bins = 30) +
theme_minimal(base_size = 12) +
labs(title = "Distribution of GDP per Capita",
x = "GDP per Capita (PPP, current international $)",
y = "Count")
Five helpful practices for writing R code are consistent, informative
object names; clear, tidy pipelines that read top to bottom; frequent
use of comments to explain non-obvious steps; setting
na.rm = TRUE deliberately so summaries do what you expect;
and keeping figures labeled with readable titles and axis names. The
advice about naming and commenting seems basic, but it makes the most
difference later when you revisit the code.
If you choose to use AI here, first paste this prompt to the AI and proceed as directed:
“You are a tutoring AI helping students work through R code and statistics. For the rest of this conversation, I will provide you a problem or question, and you will ask me leading questions, provide examples, or analogies to help me on my own, but you will not provide an immediate answer. You will ask me to explain my thinking. You can give hints, but it is better to try to help me work through the solution using leading questions and examples. You are supposed to help me learn through example, not simply provide a solution. Do you understand?”
The error occurs because summarize(mean(murderrate)) returns an unnamed column and arrange(desc) lacks a target. Naming the summary column and passing it into desc() fixes it.
states %>%
group_by(region) %>%
summarize(mean(murderrate)) %>%
arrange(desc)
states %>%
group_by(region) %>%
summarize(mean_murder = mean(murderrate, na.rm = TRUE)) %>%
arrange(desc(mean_murder))
## # A tibble: 4 × 2
## region mean_murder
## <fct> <dbl>
## 1 South 6.16
## 2 Midwest 3.62
## 3 West 3.58
## 4 Northeast 2.81
The plot runs but looks odd because the pipeline is broken before
labs() and because pid7 is better treated as a
discrete scale. Overplotting also makes it hard to see the pattern.
Converting pid7 to a factor, adding a small jitter, and
including a summary of group means produces something more readable.
ggplot(nes, aes(pid7, ftmuslim)) + geom_point() +
theme_minimal() + theme(axis.text.x = element_text(size = 8, angle = 45, vjust = .7))
labs(title = paste("Feelings Towards Muslims")) + ylab("Muslim Thermometer") + xlab("Party Identification")
ggplot(nes, aes(x = factor(pid7), y = ftmuslim)) +
geom_jitter(width = 0.2, height = 0, alpha = 0.4) +
stat_summary(fun = "mean", geom = "point", size = 3) +
theme_minimal(base_size = 12) +
theme(axis.text.x = element_text(size = 9, angle = 45, vjust = 0.7)) +
labs(title = "Feelings Towards Muslims",
x = "Party Identification (pid7)",
y = "Muslim Thermometer")
The summary and histogram of gdppc show a wide range
with a long right tail. Most countries cluster at lower income levels,
and a few very wealthy countries extend the distribution far to the
right. The mean is much larger than the median, which is another sign of
right-skew.
world %>%
summarize(
mean_gdppc = mean(gdppc, na.rm = TRUE),
median_gdppc = median(gdppc, na.rm = TRUE),
sd_gdppc = sd(gdppc, na.rm = TRUE),
n = sum(!is.na(gdppc))
)
## mean_gdppc median_gdppc sd_gdppc n
## 1 16326.78 9863.205 18456.59 176
ggplot(world, aes(x = gdppc)) +
geom_histogram(bins = 30) +
theme_minimal(base_size = 12) +
labs(title = "Distribution of GDP per Capita",
x = "GDP per Capita",
y = "Count")
Taking the natural log of gdppc compresses the high end
and spreads out the lower end. This reduces right-skew and makes
differences among lower and middle-income countries easier to see.
world <- world %>% mutate(log_gdppc = log(gdppc))
ggplot(world, aes(x = log_gdppc)) +
geom_histogram(bins = 30) +
theme_minimal(base_size = 12) +
labs(title = "Distribution of log(GDP per Capita)",
x = "log(GDP per Capita)",
y = "Count")
Taking logs is most helpful for strictly positive, right-skewed
variables measured on a ratio scale. For a variable like
health (total health expenditures as a percent of GDP), a
log can be reasonable if all values are positive and if the distribution
is also right-skewed. Since it is a percentage bounded above by 100,
interpretation should focus on proportional changes rather than absolute
differences. Zeros would be a problem for a log transform and would need
to be handled before logging.
To calculate summary statistics for gdppc only for Latin
America, filter on the region label and then summarize. This approach is
readable and reproducible.
world %>%
filter(aclpregion == "Latin America & Caribbean") %>%
summarize(
mean_gdppc = mean(gdppc, na.rm = TRUE),
median_gdppc = median(gdppc, na.rm = TRUE),
sd_gdppc = sd(gdppc, na.rm = TRUE),
n = sum(!is.na(gdppc))
)
## mean_gdppc median_gdppc sd_gdppc n
## 1 NaN NA NA 0
Per the assignment instructions, include links to share all your chat history for any AIs used for this assignment. If you did not use AI, write “No AI used.” If you did use AI, paste the share links below.