Find the current packages in the environment; list out the objects in each package; count them
lapply(lapply(search(), ls), length)
# r : the rate
# L : loan
# Y : Years
Y <- seq(10, 30, 5)
L <- c(5000000, 10000000, 15000000)
r <- c(0.02, 0.05, 0.07)
#make all combinations into data.frame
df <- expand.grid(Year = Y, Loan = L , rate = r)
df$pay <- apply(df, 1, function(x) { x[2]*(x[3]/(1-(1+x[3])^(-x[1]*12))) })
df
Year Loan rate pay
1 10 5.0e+06 0.02 110240
2 15 5.0e+06 0.02 102914
3 20 5.0e+06 0.02 100870
4 25 5.0e+06 0.02 100264
5 30 5.0e+06 0.02 100080
6 10 1.0e+07 0.02 220481
7 15 1.0e+07 0.02 205827
8 20 1.0e+07 0.02 201741
9 25 1.0e+07 0.02 200527
10 30 1.0e+07 0.02 200160
11 10 1.5e+07 0.02 330721
12 15 1.5e+07 0.02 308741
13 20 1.5e+07 0.02 302611
14 25 1.5e+07 0.02 300791
15 30 1.5e+07 0.02 300241
16 10 5.0e+06 0.05 250719
17 15 5.0e+06 0.05 250038
18 20 5.0e+06 0.05 250002
19 25 5.0e+06 0.05 250000
20 30 5.0e+06 0.05 250000
21 10 1.0e+07 0.05 501437
22 15 1.0e+07 0.05 500077
23 20 1.0e+07 0.05 500004
24 25 1.0e+07 0.05 500000
25 30 1.0e+07 0.05 500000
26 10 1.5e+07 0.05 752156
27 15 1.5e+07 0.05 750115
28 20 1.5e+07 0.05 750006
29 25 1.5e+07 0.05 750000
30 30 1.5e+07 0.05 750000
31 10 5.0e+06 0.07 350104
32 15 5.0e+06 0.07 350002
33 20 5.0e+06 0.07 350000
34 25 5.0e+06 0.07 350000
35 30 5.0e+06 0.07 350000
36 10 1.0e+07 0.07 700209
37 15 1.0e+07 0.07 700004
38 20 1.0e+07 0.07 700000
39 25 1.0e+07 0.07 700000
40 30 1.0e+07 0.07 700000
41 10 1.5e+07 0.07 1050313
42 15 1.5e+07 0.07 1050005
43 20 1.5e+07 0.07 1050000
44 25 1.5e+07 0.07 1050000
45 30 1.5e+07 0.07 1050000
dta3 <- read.table("hs0.txt", h = T)
mapply(function(i,j) { t.test(dta3[,i], dta3[,j])$p.value },
rep(7:11, each = 5),7:11) %>% matrix(5,5)
[,1] [,2] [,3] [,4] [,5]
[1,] 1.0000 0.5813 0.6728 0.7572 0.8677
[2,] 0.5813 1.0000 0.8903 0.3776 0.7150
[3,] 0.6728 0.8903 1.0000 0.4516 0.8118
[4,] 0.7572 0.3776 0.4516 1.0000 0.6378
[5,] 0.8677 0.7150 0.8118 0.6378 1.0000
若p值大於0.05,則無顯著差異。
yvar <- names(dta3)[7:11]
lapply(yvar, function(y) {
oneway.test(substitute(i ~ race, list(i = as.name(y))), data = dta3)$p.value})
[[1]]
[1] 0.0008795
[[2]]
[1] 0.0003045
[[3]]
[1] 6.491e-05
[[4]]
[1] 1.218e-05
[[5]]
[1] 0.04209
p值均小於0.05,各種族平均分數有差異。
tidy(lm(math ~ ses - 1, dta3), conf.int = TRUE) %>%
mutate(term = ifelse(term == "seshigh",
"High", ifelse(term == "sesmiddle", "Middle", "Low"))) %>%
ggplot(aes(term, estimate)) +
geom_pointrange(aes(ymin = conf.low, ymax = conf.high)) +
coord_flip() +
labs(x = "Ses", y = "Estimated mean") +
theme_bw()
circle <- function(x) {sqrt(1^2 - x^2)}
N <- 100000; x <- runif(N, 0, 1); y <- runif(N, 0, 1)
# the area of the green part
A <- sum(circle(runif(N, 0, 1)) > runif(N, 0, 1))/N*(1*1)
# circular area = πr^2; π = area/r^2
A*4/1^2
[1] 3.15
curve(circle, 0, 1, ylim = c(0, 1), ylab = "f(x)")
points(x, y, col = ifelse(circle(x) > y, 3, 2), pch = ".")