#讀取資料
dta <- read.table("hs0.txt", header = T)
#只看race為asian的資料
dta.asian <- subset(dta, race=="asian")
#看math與socst的相關係數
r0 <- cor(dta.asian$math, dta.asian$socst)
#參數設定
cnt <- 0
nIter <- 1001
#將read資料隨機排列後,與math求相關係數,重複1001次
#並記錄read與math的相關係數大於等於math與socst的相關係數的次數
#最後算出比例
for (i in 1:nIter) {
new <- sample(dta.asian$read)
r <- cor(new, dta.asian$math)
if ( r0 <= r ) cnt <- cnt+1
}
cnt/nIter
## [1] 0.03796204
不使用for迴圈的寫法
dta <- read.table("hs0.txt", header = T)
dta.asian <- subset(dta, race=="asian")
r0 <- cor(dta.asian$math, dta.asian$socst)
cnt <- 0
nIter <- 1001
i <- 1
repeat{
new <- sample(dta.asian$read)
r <- cor(new, dta.asian$math)
i=i+1
if ( r0 <= r ) cnt <- cnt+1
if (i>=nIter) break}
cnt/nIter
## [1] 0.05594406
跑出一樣本數為n的布朗運動圖nIter張
Brownian <- function(n = 20, pause = 0.05, nIter = 3, ...) {
x = rnorm(n)
y = rnorm(n)
i = 1
while (i <= nIter) {
plot(x, y, ...)
text(x, y, cex = 0.5)
x = x + rnorm(n)
y = y + rnorm(n)
Sys.sleep(pause)
i = i + 1
}
}
### test it
Brownian(xlim = c(-20, 20), ylim = c(-20, 20),
pch = 21, cex = 2, col = "cyan", bg = "lavender")
不使用for迴圈的寫法
Brownian <- function(n = 20, pause = 0.05, nIter = 3, ...) {
x = rnorm(n)
y = rnorm(n)
i = 1
repeat {
plot(x, y, ...)
text(x, y, cex = 0.5)
x = x + rnorm(n)
y = y + rnorm(n)
Sys.sleep(pause)
i = i + 1
if(i >= nIter) break
}
}
Brownian(xlim = c(-20, 20), ylim = c(-20, 20),
pch = 21, cex = 2, col = "cyan", bg = "lavender")
隨機產生一個n*2的矩陣資料,第一行為性別(男女),第二行為身高(公分)
出現男生機率為0.505,出現女生機率為0.495
男生身高平均數為170標準差為7
女生身高平均數為160標準差為5
simSexHeight <- function(n) {
m <- matrix(nrow=n, ncol=2)
m[, 1] <- runif(n)
m[, 2] <- rnorm(n)
draw <- function(mr) { # mr = one row of m
if (mr[1] < 0.505) {
sex <- 1
cm <- 170 + 7*mr[2]
} else {
sex <- 0
cm <- 160 + 5*mr[2]
}
return(c(sex, cm))
}
person <- t(apply(m, 1, draw))
person <- as.data.frame(person)
person[, 1] <- ifelse(person[, 1]==1, "M", "F")
names(person) <- c("Gender", "Height")
return(person)
}
simSexHeight(5)
## Gender Height
## 1 M 177.9718
## 2 F 163.7873
## 3 F 161.1460
## 4 M 176.4151
## 5 M 174.7751
improve
NEW_simSexHeight <- function(n){
m <- matrix(nrow=n, ncol=2)
m[, 1] <- runif(n)
m[, 2] <- rnorm(n)
Gender <- ifelse(m[, 1] < 0.505, "M", "F")
Height <- ifelse(m[, 1] < 0.505, 170 + 7*m[, 2],160 + 5*m[, 2])
Person <- data.frame(Gender,Height)
return(Person)
}
NEW_simSexHeight(5)
## Gender Height
## 1 M 181.1997
## 2 F 170.4683
## 3 F 157.4666
## 4 M 173.2316
## 5 M 174.9778