R語言-《Learning R》-Chapter15 : Distribution and Modeling-隨機數字+線性迴歸
阿新 • • 發佈:2018-12-21
1. Random Numbers(隨機數字)
## 隨機數:從1到7的7個隨機數
> sample(7)
[1] 5 2 7 4 3 6 1
## 隨機數:從1到7的5個隨機數
> sample(7, 5)
[1] 7 2 6 3 4
> sample(7, 10, replace = TRUE)
[1] 7 3 5 2 7 5 2 3 4 5
> sample(.leap.seconds, 4)
[1] "1991-01-01 08:00:00 CST"
[2] "1999-01-01 08:00:00 CST"
[3] "1992-07-01 08:00:00 CST"
[4] "1988-01-01 08:00:00 CST"
> sample(colors(), 4)
[1] "darkgoldenrod3" "mediumorchid"
[3] "indianred2" "gold2"
## 隨機數並且限定暑假的概論(13, 21)最大
> weights <- c(1, 1, 2, 3, 5, 8, 13, 21, 8, 3, 1, 1)
> sample(month.abb, 1, prob = weights)
[1] "Jun"
> sample(month.abb, 1, prob = weights)
[1] "Dec"
> sample (month.abb, 1, prob = weights)
[1] "Jul"
## 5 uniform random numbers between 0 and 1
runif(5)
## 5 uniform random numbers between 1 and 10
runif(5, 1,10)
## 5 normal random numbers with mean 0 and std dev 1
rnorm(5)
## 5 normal random numbers with mean 3 and std dev 7
rnorm(5, 3, 7)
## 隨機數的演算法
> ? RNG
> RNGkind()
[1] "Mersenne-Twister"
[2] "Inversion"
## 設定seed,確定每次的隨機數字
> set.seed(105)
> runif(5)
[1] 0.09791495 0.99563352
[3] 0.32055252 0.39772641
[5] 0.55322525
> set.seed(105)
> runif(5)
[1] 0.09791495 0.99563352
[3] 0.32055252 0.39772641
[5] 0.55322525
## You can also specify different generation algorithms,
though this is very advanced usage,so don’t do that
unless you know what you are doing.
2. A First Model: Linear Regressions(線性迴歸)
> library(learningr)
> gonorrhoea
## The function for calculating them is concisely if not clearly named:lm, short for “linear model.”
## 檢視分組變數的levels
> lapply(Filter(is.factor, gonorrhoea), levels)
$`Age.Group`
[1] "0 to 4" "5 to 9" "10 to 14" "15 to 19"
[5] "20 to 24" "25 to 29" "30 to 34" "35 to 39"
[9] "40 to 44" "45 to 54" "55 to 64" "65 or more"
$Ethnicity
[1] "American Indians & Alaskan Natives"
[2] "Asians & Pacific Islanders"
[3] "Hispanics"
[4] "Non-Hispanic Blacks"
[5] "Non-Hispanic Whites"
$Gender
[1] "Female" "Male"
> model1 <- lm(Rate ~ Year + Age.Group + Ethnicity + Gender, gonorrhoea)
> model1
Call:
lm(formula = Rate ~ Year + Age.Group + Ethnicity + Gender, data = gonorrhoea)
Coefficients:
(Intercept)
5540.496
Year
-2.770
Age.Group5 to 9
-0.614
Age.Group10 to 14
15.268
Age.Group15 to 19
415.698
Age.Group20 to 24
546.820
Age.Group25 to 29
291.098
Age.Group30 to 34
155.872
Age.Group35 to 39
84.612
Age.Group40 to 44
49.506
Age.Group45 to 54
27.364
Age.Group55 to 64
8.684
Age.Group65 or more
1.178
EthnicityAsians & Pacific Islanders
-82.923
EthnicityHispanics
-49.000
EthnicityNon-Hispanic Blacks
376.204
EthnicityNon-Hispanic Whites
-68.263
GenderMale
-17.892