簡體   English   中英

在R中模擬2階最小二乘回歸

[英]Simulating a 2-Stage Least Squares regression in R

我對R真的很陌生,必須在上課時完成以下任務:

本練習顯示了兩階段最小二乘方中弱儀器的有限樣本問題。 令結構方程為yi = beta1 + beta2 * x_i + eta_i

而簡化形式的方程為xi = gamma1 + gamma2 * z_i + e_i:

設樣本為iid,z_i〜N(0; 1),(eta_i e_i)〜N((0 0),(1 0.5 1 0.5))

將真實參數值設置為beta1 = 1; beta2 = 2,gamma1 = 0。

對於下面的每個n和gamma2組合,請使用標准的兩階段最小二乘估計器來估計beta2(自行寫下估計器。)重復此實驗1000次,這樣我們就有1000個估計的beta2。

  1. n = 100且gamma2 = 1。
  2. n = 100且gamma2 = 0:1。
  3. n = 100且gamma2 = 0:01。
  4. n = 1000,且gamma2 = 0:01。

在每種情況下繪制這些beta2的內核密度,以便我們可以看到采樣分布。 使用ggplot將4個子圖放入一個圖。

我花了幾個小時試圖弄清楚如何將其轉換為有效的模擬。

我的最后一個解決方案是這樣的:

#We will simulate data for this exercise

rm(list = ls( ) )

options(max.print=999999)

#4 different groups of parameters, only n and gamma2 change

  #First Estimation: n=100 and gamma2=1


set.seed(1313)
  #We write a function of the regression model, to later replicate that function
  #and receive 1000 simulations of it.
  b2hat_1_f = function(){
  n = 100 
  #b1 is the constant. We therefore assign it a number of values equal to n.
  b1 = matrix(1, nrow = 100)
  b2 = matrix(2, nrow = 2)
  gamma1 = matrix(0, nrow = 100)
  gamma2 = matrix(1, nrow = 2)

  #Generate Data
  #We have to take the square root of the given variance matrix as rnorm in R uses the standard deviation.
  eta_i = rnorm(n, mean = matrix(0, nrow = 2), sd = sqrt(matrix(c(1, 0.5, 0.5, 1), nrow = 2)))
  e_i = rnorm(n, mean = matrix(0, nrow = 2), sd = sqrt(matrix(c(1, 0.5, 0.5, 1), nrow = 2)))
  Z = cbind(1, rnorm(n, mean = 0, sd = 1))
  X = gamma1 + Z %*% gamma2 + e_i
  X_hat = cbind(1,X)
  Y = b1 + X_hat %*% b2 + eta_i

  #Estimate b2_hat
  #Formula for beta2 estimator is beta_hat_IV = (Z'X)^-1*Z'Y
  #This translates to 
  b2hat = solve((t(Z)%*%X_hat), t(Z)%*%Y)
  }

  b2hat_1 = replicate(1000, b2hat_1_f())

  #Plot the kernel density of our estimator
  plot(density(b2hat_1), main="Kernel Density of b2hat_1 Estimator")

  #Save data of estimator
  save(b2hat_1,file="b2hat_1.RData")` 

然后,我對其他三種情況有類似的設置,最后嘗試將它們組合成這樣的圖形:

#Combine 4 subgraphs using ggplot2
  library(ggplot2)
  library(reshape2)

  load("b2hat_comb.RData")

  b2s = b2hat_comb[, c("b2hat_1", "b2hat_2", "b2hat_3", "b2hat_4")]
  print(head(b2s))

  #Put estimators into data.frame format for use of ggplot2
  x1 = data.frame(b2hat_1)
  x2 = data.frame(b2hat_2)
  x3 = data.frame(b2hat_3)
  x4 = data.frame(b2hat_4)

  ggplot() +
    # b2hat_1
    geom_density(data=x1, aes(x=x1),colour="blue", size=1) +

    # b2hat_2
    geom_density(data=x2, aes(x=x2) ,colour="red", size=1) +

    #b2hat_3
    geom_density(data=x3, aes(x=x3) ,colour="red", size=1) +

    #b2hat_4
    geom_density(data=x4, aes(x=x4) ,colour="red", size=1)

現在我知道該模型的建立可能是錯誤的,並且部分原因是由於缺乏計量經濟學知識和對r的誤解。 我只是不知道該如何進行,目前正在接近“危機模式”。 如果你們中的一個人可以抽出時間看一下我在做錯什么,我將cbind ,例如,對於我來說X_hatcbind選項似乎不正確。 另外,我不確定是否為beta2使用正確的公式。

我意識到該頁面也不應幫助我完成學業,但是ggplot2部分實際上是與r相關的問題,因為我不知道自己在制作4個估計量的組合密度曲線時做錯了什么。 我嘗試了不同的代碼,但是都沒有用。 在這里,我收到錯誤消息:

Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (2): x

非常感謝您對我的疑問提出的歉意。 如有必要,我將很樂意澄清。

好的,我假設我正確地處理了數據,但是您可以隨意更正我。 我更改了函數的輸出,以生成一個1x2矩陣,合並復制的矩陣,並使用該矩陣創建具有兩個變量的數據框。 然后,我通過賦值添加了代表數據集名稱(x1)的第三個變量。 然后,我將數據框ggplot()傳遞給它,並使用帶有透明度的geom_density繪制變量x。 它被編寫為接受所有四個組(x1:x4),並且應該為每個組生成圖:

b2hat_1_f = function(){
  n = 100 
  #b1 is the constant. We therefore assign it a number of values equal to n.
  b1 = matrix(1, nrow = 100)
  b2 = matrix(2, nrow = 2)
  gamma1 = matrix(0, nrow = 100)
  gamma2 = matrix(1, nrow = 2)

  #Generate Data
  #We have to take the square root of the given variance matrix as rnorm in R uses the standard deviation.
  eta_i = rnorm(n, mean = matrix(0, nrow = 2), sd = sqrt(matrix(c(1, 0.5, 0.5, 1), nrow = 2)))
  e_i = rnorm(n, mean = matrix(0, nrow = 2), sd = sqrt(matrix(c(1, 0.5, 0.5, 1), nrow = 2)))
  Z = cbind(1, rnorm(n, mean = 0, sd = 1))
  X = gamma1 + Z %*% gamma2 + e_i
  X_hat = cbind(1,X)
  Y = b1 + X_hat %*% b2 + eta_i

  #Estimate b2_hat
  #Formula for beta2 estimator is beta_hat_IV = (Z'X)^-1*Z'Y
  #This translates to 
  b2hat = solve((t(Z)%*%X_hat), t(Z)%*%Y)
  m <- t(b2hat)
}

b2hat_1 = replicate(1000, b2hat_1_f())

m <- b2hat_1
m <- matrix(data = m , ncol=2)
df <- data.frame(x = m[,1], y = m[,2])
df$group <- "x1"

##with the 'group' variable, you don't need separate geoms
library(ggplot2)
ggplot() +
  geom_density(data = df, aes(x = x,  fill = group),alpha=0.5)

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM