##week4
"str 函数"
#返回参数
# > str(str)
# function (object, ...)
# > str(lm)
# function (formula, data, subset, weights, na.action, method = "qr",
# model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
# contrasts = NULL, offset, ...)
# > str(ls)
# function (name, pos = -1L, envir = as.environment(pos),
# all.names = FALSE, pattern, sorted = TRUE)
#
# > x <- rnorm(100,2,4)
# > summary(x)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# -7.6173 -0.5374 2.1654 2.1999 4.8181 12.2632
# > str(x)
# num [1:100] 5.25 -3.75 4.06 8.18 -0.28 ...
# > f <- gl(40,10)
# > str(f) 输出会更加紧凑
# Factor w/ 40 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
# > library(datasets)
# > head(airquality)
# Ozone Solar.R Wind Temp Month Day
# 1 41 190 7.4 67 5 1
# 2 36 118 8.0 72 5 2
# 3 12 149 12.6 74 5 3
# 4 18 313 11.5 62 5 4
# 5 NA NA 14.3 56 5 5
# 6 28 NA 14.9 66 5 6
# > str(airquality)
# 'data.frame': 153 obs. of 6 variables:
# $ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
# $ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
# $ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
# $ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
# $ Month : int 5 5 5 5 5 5 5 5 5 5 ...
# $ Day : int 1 2 3 4 5 6 7 8 9 10 ...
# > library(datasets)
# > head(airquality)
# Ozone Solar.R Wind Temp Month Day
# 1 41 190 7.4 67 5 1
# 2 36 118 8.0 72 5 2
# 3 12 149 12.6 74 5 3
# 4 18 313 11.5 62 5 4
# 5 NA NA 14.3 56 5 5
# 6 28 NA 14.9 66 5 6
# > str(airquality)
# 'data.frame': 153 obs. of 6 variables:
# $ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
# $ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
# $ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
# $ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
# $ Month : int 5 5 5 5 5 5 5 5 5 5 ...
# $ Day : int 1 2 3 4 5 6 7 8 9 10 ...
"总结:str相当于一个概括的函数"
"simulation"
# 四中随机数
dnorm(x, mean = 0,sd= 1, log = FALSE)
pnorm(q, mean = 0,sd = 1, lower.tail = TRUE, log.p = FALSE)
qnorm(p, mean = 0,sd = 1, lower.tail = TRUE, log.p = FALSE)
rnorm(n, mean = 0,sd = 1)
# > x <- rnorm(10)
# > x
# [1] 0.6655806 -0.4015949 -2.7861818 -1.5521880 1.9839463
# [6] -0.8560313 0.8182983 -0.2929819 1.4166468 -1.7064836
# > x <- rnorm(10, 20, 2)
# > x
# [1] 18.15547 17.62154 17.20510 17.75022 17.79313 18.99929
# [7] 21.45195 20.91455 22.81174 20.40405
# > summary(x)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 17.21 17.76 18.58 19.31 20.79 22.81
# R中生成的数字并不是完全随机的,设置seed方便追溯结果
# > set.seed(1)
# > rnorm(5)
# [1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078
# > rnorm(5)
# [1] -0.8204684 0.4874291 0.7383247 0.5757814 -0.3053884
# > set.seed(1)
# > rnorm(1)
# [1] -0.6264538
# > rpois(10,1)
# [1] 1 2 0 2 3 1 1 0 0 0
# > rpois(10,2)
# [1] 3 1 3 2 3 6 1 3 4 1
# > rpois(10,20)
# [1] 21 17 18 18 21 16 23 22 24 23
# >
# > ppois(2,2)
# [1] 0.6766764
# > ppois(4,2)
# [1] 0.947347
# > ppois(6,2)
# [1] 0.9954662
# > rpois(10,1)
# [1] 1 2 0 2 3 1 1 0 0 0
# > rpois(10,2)
# [1] 3 1 3 2 3 6 1 3 4 1
# > rpois(10,20)
# [1] 21 17 18 18 21 16 23 22 24 23
# >
# > ppois(2,2) cumulative distribution
# [1] 0.6766764 ##Pr(x <= 2)
# > ppois(4,2)
# [1] 0.947347 ## Pr(x <= 4)
# > ppois(6,2)
# [1] 0.9954662 ## Pr(x <= 6)
"画图 生成一个随机函数"
set.seed(20)
x <- rnorm(100)
e <- rnorm(100,0, 2)
y <- 0.5 + 2 * x + e
summary(y)
plot(x, y)
# > set.seed(20)
# > x <- rnorm(100)
# > e <- rnorm(100,0, 2)
# > y <- 0.5 + 2 * x + e
# > summary(y)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# -6.4084 -1.5402 0.6789 0.6893 2.9303 6.5052
# > plot(x, y)
# 如果是两元的函数
set.seed(10)
x <- rbinom(100, 1, 0.5)
e <- rnorm(100, 0, 2)
y <- 0.5 + 2 * x + e
summary(y)
plot(x,y)
# > set.seed(10)
# > x <- rbinom(100, 1, 0.5)
# > e <- rnorm(100, 0, 2)
# > y <- 0.5 + 2 * x + e
# > summary(y)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# -3.4936 -0.1409 1.5767 1.4322 2.8397 6.9410
# > plot(x,y)
set.seed(1)
x <- rnorm(100)
log.mu <- 0.5 +0.3 * x
y <- rpois(100, exp(log.mu))
summary(y)
plot(x,y)
# > set.seed(1)
# > x <- rnorm(100)
# > log.mu <- 0.5 +0.3 * x
# > y <- rpois(100, exp(log.mu))
# > summary(y)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 0.00 1.00 1.00 1.55 2.00 6.00
# > plot(x,y)
# sample 随机取出四个数字且不放回
# 除了对数字取样还能对字母取样
# > set.seed(1)
# > sample(1:10, 4)
# [1] 9 4 7 1
# > sample(1:10, 4)
# [1] 2 7 3 6
# > sample(letters, 5)
# [1] "r" "s" "a" "u" "w"
# > sample(1:10)
# [1] 10 6 9 2 1 5 8 4 3 7
# > sample(1:10)
# [1] 5 10 2 8 6 1 4 3 9 7
# > sample(1:10,replace= TRUE)
# [1] 3 6 10 10 6 4 4 10 9 7
"profiler 分析为什么时间过程的工具"
# user time = cpu + system time
# elapsed time = user time + 加载时间
system.time({
n <- 1000
r <- numeric(n)
for (i in 1:n){
x<- rnorm(n)
r[i] <- mean(x)
}
})
"R profiler"
R学习——霍普金斯大学week4
最新推荐文章于 2025-01-16 14:57:06 发布
216

被折叠的 条评论
为什么被折叠?



