问题描述
您好,我从课程中获得以下代码
library(tidyverse)
library(dslabs)
data("polls_us_election_2016")
head(results_us_election_2016)
results_us_election_2016 %>% arrange(desc(electoral_Votes)) %>% top_n(5,electoral_Votes)
'Computing the average and standard deviation for each state'
polls <- polls_us_election_2016 %>%
filter(state != "U.S." &
!grepl("CD","state") &
enddate >= "2016-10-31" &
(grade %in% c("A+","A","A-","B+") | is.na(grade))) %>%
mutate(spread = rawpoll_clinton/100 - rawpoll_trump/100) %>%
group_by(state) %>%
summarize(avg = mean(spread),sd = sd(spread),n = n()) %>%
mutate(state = as.character(state))
# joining electoral college Votes and results
results <- left_join(polls,results_us_election_2016,by="state")
head(results)
# states with no polls: note Rhode Island and district of Columbia = Democrat
results_us_election_2016 %>% filter(!state %in% results$state)
# assigns sd to states with just one poll as median of other sd values
results <- results %>%
mutate(sd = ifelse(is.na(sd),median(results$sd,na.rm = TRUE),sd))
#Calculating the posterior mean and posterior standard error
mu <- 0
tau <- 0.02
results %>% mutate(sigma = sd/sqrt(n),B = sigma^2/ (sigma^2 + tau^2),posterior_mean = B*mu + (1-B)*avg,posterior_se = sqrt( 1 / (1/sigma^2 + 1/tau^2))) %>%
arrange(abs(posterior_mean))
#Monte Carlo simulation of Election Night results (no general bias)
mu <- 0
tau <- 0.02
clinton_EV <- replicate(1000,{
results %>% mutate(sigma = sd/sqrt(n),posterior_se = sqrt( 1 / (1/sigma^2 + 1/tau^2)),simulated_result = rnorm(length(posterior_mean),posterior_mean,posterior_se),clintonVotes = ifelse(simulated_result > 0,electoral_Votes,0)) %>% # award Votes if Clinton wins state
summarize(clinton = sum(clintonVotes)) %>% # total Votes for Clinton
.$clinton + 7 # 7 Votes for Rhode Island and DC
})
mean(clinton_EV > 269) # over 269 Votes wins election
我不明白这条线是如何工作的
simulated_result = rnorm(length(posterior_mean),posterior_se)
length(posterior_mean) = 47
,因此rnorm
应该返回大小为47的向量。
当我将其替换为1时,尽管posterior_mean和posterior_se对于每个状态都是不同的,但是每个状态都从rmrm获得相同的结果。更改它46时出现错误。
所以在我看来,这行充满了整个列simulated_result(也许有相同结果的47次?)。我本来希望mutate仅使用每一行的值来操纵此特定行。
也许有人可以向我解释这种行为或将我指向解释该现象的资源吗?
解决方法
对于rnorm
功能,如果您检查了小插图:
rnorm(n,mean = 0,sd = 1) Arguments
x,q :vector of quantiles.
p :vector of probabilities.
n :number of observations. If length(n) > 1,the length is taken to be the number required.
mean :vector of means.
sd :vector of standard deviations.
有两种使用方法,一种是生成一个长度为n的向量,该向量来自均值和sd相同的正态分布,例如:
set.seed(111)
rnorm(10,1)
[1] 0.2352207 -0.3307359 -0.3116238 -2.3023457 -0.1708760 0.1402782 -1.4974267 -1.0101884
[9] -0.9484756 -0.4939622
如果提供的向量长为n,则为每个条目指定均值和sd,例如:
set.seed(111)
rnorm(10,1:10,1:10)
[1] 1.23522071 1.33852826 2.06512853 -5.20938263 4.14561978 6.84166935 -3.48198659 -0.08150735
[9] 0.46371956 5.06037783
在这种情况下,您将生成一个包含10个随机正态变量的向量,第一个条目来自均值= 1,sd = 1,第二个条目均值= 2,sd = 2,依此类推。我们还可以在两者之间做一些事情:
set.seed(111)
rnorm(10,1))
[1] 1.235221 1.669264 2.688376 1.697654 4.829124 6.140278 5.502573 6.989812 8.051524 9.506038
在这种情况下,它返回一个长度为10的向量,第一个条目来自均值= 1,sd = 1,第二个条目来自均值= 2,sd = 1,我们可以通过重新运行它来可视化它: / p>
t(replicate(10,rnorm(10,1)))
用1代替什么不是很清楚,但本质上mutate的目的是为列分配值。模拟结果列的工作原理与上述相同。