Question 1

lapply(lapply(search(), ls), length)
## [[1]]
## [1] 0
## 
## [[2]]
## [1] 236
## 
## [[3]]
## [1] 117
## 
## [[4]]
## [1] 31
## 
## [[5]]
## [1] 81
## 
## [[6]]
## [1] 403
## 
## [[7]]
## [1] 151
## 
## [[8]]
## [1] 237
## 
## [[9]]
## [1] 447
## 
## [[10]]
## [1] 87
## 
## [[11]]
## [1] 107
## 
## [[12]]
## [1] 211
## 
## [[13]]
## [1] 104
## 
## [[14]]
## [1] 218
## 
## [[15]]
## [1] 0
## 
## [[16]]
## [1] 1217
#內層的lapply使用search列出所有物件,並以list格式輸出
#外層的lapply使用length算出物件總數,並以list格式輸出
#計算ls的物件數量

Question 2

payment <- function(y){
  m<-12*y
  l<-seq(5000000,15000000,5000000)
  r<-c(0.02,0.05,0.07)
  p<-outer(l,r/(1-(outer((1+r),(-m),"^"))),"*")
  return(p)
}
mapply(payment,y=seq(10,30,5))
##            [,1]      [,2]      [,3]      [,4]      [,5]
##  [1,]  110240.5  102913.7  100870.4  100263.7  100080.2
##  [2,]  220481.0  205827.4  201740.8  200527.4  200160.4
##  [3,]  330721.5  308741.0  302611.2  300791.1  300240.7
##  [4,]  250718.6  250038.4  250002.1  250000.1  250000.0
##  [5,]  501437.1  500076.7  500004.1  500000.2  500000.0
##  [6,]  752155.7  750115.1  750006.2  750000.3  750000.0
##  [7,]  350104.3  350001.8  350000.0  350000.0  350000.0
##  [8,]  700208.5  700003.6  700000.1  700000.0  700000.0
##  [9,] 1050312.8 1050005.4 1050000.1 1050000.0 1050000.0

Question 3

(a)

#讀資料
setwd("/Users/tayloryen/Desktop/大學/成大課業/大四下/資料管理/0409")
dta<-read.table("hs.txt",header = T)
#兩兩作t檢定
outer(7:11,7:11,Vectorize(function (i,j){t.test(dta[,i], dta[,j])$p.value}))
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 1.0000000 0.5812665 0.6728327 0.7572410 0.8676815
## [2,] 0.5812665 1.0000000 0.8903494 0.3775863 0.7150322
## [3,] 0.6728327 0.8903494 1.0000000 0.4515844 0.8118467
## [4,] 0.7572410 0.3775863 0.4515844 1.0000000 0.6377587
## [5,] 0.8676815 0.7150322 0.8118467 0.6377587 1.0000000

(b)

library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:Matrix':
## 
##     expand
library(ggplot2)
dta %>%
  gather(subject, score, 7:11) %>% 
  ggplot(., aes(race, score, color = subject, group = subject))+
  stat_summary(fun.data = mean_se,
               position = position_dodge(.5),
               na.rm = TRUE)+
  theme(legend.position = c(.8, .1), legend.direction = "horizontal")+
  theme_bw()

(c)

lm(math ~ -1 + ses, data = dta)
## 
## Call:
## lm(formula = math ~ -1 + ses, data = dta)
## 
## Coefficients:
##   seshigh     seslow  sesmiddle  
##     56.17      49.17      52.21
ggplot(dta, aes(ses, math))+
  stat_summary(fun.data = mean_cl_boot, na.rm = TRUE)+
  scale_x_discrete(limits = c("low", "middle", "high"))+
  scale_y_continuous(breaks = seq(40, 70, by = 2.5))+
  labs(x = "SES", y = "Average Math Score")

Question 4

circle_data<-data.frame(x_val=c(0,seq(0,1,length.out=1000)),
                        y_val=c(0,sqrt(1-seq(0,1,length.out=1000)^2)))
plot_pi <- ggplot() +
geom_polygon(data=circle_data,aes(x=x_val,y=y_val),alpha=0.1) + theme_bw()
plot_pi

dot_data<-data.frame(x_val=runif(25),y_val=runif(25))
dot_data$in_or_out<-ifelse(sqrt(dot_data$x_val^2+dot_data$y_val^2)<=1,1,0)

plot_pi + geom_point(data=dot_data,aes(x=x_val,y=y_val,color=in_or_out)) +
  theme(legend.position="none")

dot_data_2<-data.frame(x_val=runif(10^4),y_val=runif(10^4))
dot_data_2$in_or_out<-ifelse(sqrt(dot_data_2$x_val^2+dot_data_2$y_val^2)<=1,1,0)
plot_pi + geom_point(data=dot_data_2,aes(x=x_val,y=y_val,color=in_or_out)) +
theme(legend.position="none")

4*sum(dot_data_2$in_or_out)/nrow(dot_data_2)
## [1] 3.1124

The End