Q6 Cities and population data

讀入資料

fL <- paste0("http://", upw, "@140.116.183.121/~sheu/dataM/Data/cities10.txt")
fwf_empty(fL)[1:2]
$begin
[1]  0 19

$end
[1] 17 NA
dta6 <- read.fwf(fL, width = c(19, 8), n = 10, col.names = c("city", "population_density"))
dta6$population_density <- as.numeric(gsub(",", "", as.character(dta6$population_density)))

作圖

ggplot(dta6, aes(x= city, y = population_density)) + 
  geom_bar(stat="identity", fill="steelblue") +
  geom_text(aes(label=population_density), hjust= -0.1, size=3.5) +
  coord_flip() 

Q7 RT Data

讀入資料

fL <- paste0("http://", upw, "@www.stat.columbia.edu/~gelman/book/data/schiz.asc")
dta7 <- read.table(fL, h= F, skip = 4, sep = " ") 

整理資料

dta7<- dta7 %>% 
  mutate(schizo = c(rep("N",11), rep("Y", 6)), ID = paste0("S",1:17)) %>%
  gather(key = "trial", value = "RT", 1:30) %>%
  mutate(ID = factor(ID), schizo = factor(schizo), trial = factor(trial))

作圖

dta7$ID <- factor(dta7$ID, levels = c(paste0("S",1:17)))
ggplot(dta7, aes(x = ID, y = RT, color = schizo)) +
  geom_boxplot() +
  labs(x ="Subject", y = "Reaction Time(ms)")

分析

m0 <- aov(RT ~ schizo + ID + trial, data = dta7)
summary(m0)
             Df   Sum Sq Mean Sq F value Pr(>F)    
schizo        1  4506212 4506212 204.034 <2e-16 ***
ID           15  2865353  191024   8.649 <2e-16 ***
trial        29   638735   22025   0.997  0.472    
Residuals   464 10247711   22086                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

個體間與是否為精神病的受試者有反應時間上的差異。

Q8 Number of student in each major

讀入資料

#read header and data separately
headers = read.csv("ncku_roster.csv", header = F, nrows = 1, as.is = T)
dta8 <- read.csv("ncku_roster.csv", h= F, skip = 2)
colnames(dta8) <- headers

整理資料

作圖

ggplot(dta8, aes(major, ..count..))+
  geom_bar(fill = "#ffc34d") +
  labs(x = "Departments", y = "Count")