2017-06-17 172 views
1

我的問題是:如何生成兩個時間序列從下列數據生成過程中產生的100長度的:使用R做蒙特卡羅模擬,解決以下問題

xt = xt−1 + et, 
yt = yt−1 + ut, 

其中etut是相互獨立的標準正常的iid系列。在包含截距的上的yt的迴歸中,模擬拒絕零假設的概率,即斜率係數α爲零,公稱大小爲5%。

  1. 對α= 0,0.5,0.9,0.95,0.99的不同值進行模擬。
  2. 討論你的結果。隨着阿爾法值的變化,拒絕概率如何變化?

我的想法是,我通過編碼

y <- arima.sim(100,model=list(ar=0)) 
x <- arima.sim(100,model=list(ar=0)) 

然後做迴歸Ÿx和存儲的α的值,並重覆上述步驟,爲1000倍生成兩個華宇系列,並找到阿爾法的分佈。 但我的問題是:

  1. 我不知道如何存儲阿爾法值
  2. 如何重複迴歸1000次

我R的新學員,希望有人可以通過編寫R代碼告訴我如何解決這個問題。謝謝!

回答

1

一個tidyverse解決方案:

library(dplyr) 
library(broom) 
library(purrr) 
library(ggplot2) 

# define a function the returns the alpha -- its point estimate, standard error, etc. -- from the OLS 
iteration <- function() { 
    y <- arima.sim(100,model=list(ar=0)) 
    x <- arima.sim(100,model=list(ar=0)) 
    lm(y~x) %>% 
    broom::tidy() %>% 
    filter(term == 'x') 
} 

# 1000 iterations of the above simulation 
alphas <- map_df(1:1000, ~iteration()) 

# plot the results 
alphas %>% 
    ggplot(aes(estimate)) + geom_density() 
+0

謝謝!這正是我想要的!我成功繪製了估計的alpha的密度圖。但是我發現我不知道如何計算每個alpha值的拒絕可能性。你知道如何計算使用R的拒絕可能性嗎?或者我怎樣才能列出這個密度函數的一些詳細信息? – Mandy