简体   繁体   English

显示 1000 次模拟的 β̂ 1 OLS 和 β̂ 1 ROLS 的 kernel 密度估计

[英]showing the kernel density estimates of β̂ 1 OLS and β̂ 1 ROLS for your 1000 simulations

I have the following problem: I did 1000 simultaions to get a vector of ß^1_OLS and ß^1_ROLS, now I have to compare these two by plotting them both as a density function (I have to do it with ggplot()).我有以下问题:我做了 1000 次模拟以获得 ß^1_OLS 和 ß^1_ROLS 的向量,现在我必须通过将它们都绘制为密度 function 来比较这两者(我必须用 ggplot() 来做)。 Is it possible to plot a density from a vector?是否可以从向量中获得密度 plot ?

Following my code, please excuse the mess:按照我的代码,请原谅混乱:

>N=10000
X <- runif(N, min = 0, max = 100)
E <- runif(N, min = -5, max = 5)

U = E * sqrt(X)

# population regression
Y <- 3 + 2 * X + U
population <- data.frame(X, Y)


# set sample size an repetition 
n <- 1000
reps<- 1000



# initialize the matrix of outcomes
fit_coef <- matrix(ncol = 2, nrow = reps)
fit_coef_ROLS<-matrix(ncol = 2, nrow = reps)


# #######loop sampling and estimation of the coefficients for OLS
set.seed(1)
for (i in 1:reps){
  
  sample <- population[sample(1:N, n), ]
  fit_coef[i,] <- lm(Y ~ X, data = sample)$coefficients
  
}
fit_coef_ß1<- fit_coef[,-1] 
fit_coef_ß1
  
#######loop sampling and estimation of the robust coefficients ROLS
set.seed(1)
for (i in 1:reps){
  
  sample <- population[sample(1:N, n), ]
  fit_coef_ROLS[i,] <- rlm(Y ~ X, data = sample)$coefficients
  
}
fit_coef_ß1_ROLS<- fit_coef_ROLS[,-1] 
fit_coef_ß1_ROLS
 

## Plot
df_coef_OLS<-as.data.frame(fit_coef)

plot_coef_OLS<-ggplot(df_coef_OLS, aes(x = fit_coef_ß1)) +
  geom_density()
plot_coef_OLS

I get a plot if I use this formular but i dont know how I can get the density of both estimators in 1 Plot, also the density is not in the range of [0,1]如果我使用这个公式,我会得到一个 plot 但我不知道如何在 1 Plot 中获得两个估计器的密度,密度也不在 [0,1] 的范围内

I am thankfull for any support!我很感谢任何支持! This is the task这是任务

Sure you just need to reshape your data to have the long format, ie the relevant coefficient of every iteration gets a row in a data.frame.当然,您只需要重塑您的数据以具有长格式,即每次迭代的相关系数在 data.frame 中获得一行。 You then need another column to declare from which simulation the row came from.然后,您需要另一列来声明该行来自哪个模拟。

library(ggplot2)

## Your code goes here (omitted for brevity)

df <- data.frame(
  coef = c(fit_coef[, -1], fit_coef_ROLS[, -1]),
  type = rep(c("OLS", "ROLS"), c(nrow(fit_coef), nrow(fit_coef_ROLS)))
)

ggplot(df, aes(coef, colour = type)) +
  geom_density()

Created on 2021-12-18 by the reprex package (v2.0.1)reprex package (v2.0.1) 于 2021 年 12 月 18 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM