如何在R中运行蒙特卡罗模拟多元回归?



我想对预测mpg的多元回归模型运行蒙特卡罗模拟,然后评估每辆车比另一辆车性能更好(mpg更低)的次数。这是我目前得到的

library(pacman)
pacman::p_load(data.table, fixest, stargazer, dplyr, magrittr)
df <- mtcars
fit <- lm(mpg~cyl + hp, data = df)
fit$coefficients[1]
beta_0 = fit$coefficients[1] # Intercept 
beta_1 = fit$coefficients[2] # Slope (cyl)
beta_2 = fit$coefficients[3] # slope (hp)
set.seed(1)  # Seed
n = 1000     # Sample size
M = 500      # Number of experiments/iterations
## Storage 
slope_DT <- rep(0,M)
slope_DT_2 <- rep(0,M)
intercept_DT <- rep(0,M)
## Begin Monte Carlo
for (i in 1:M){ #  M is the number of iterations

# Generate data
U_i = rnorm(n, mean = 0, sd = 2) # Error
X_i = rnorm(n, mean = 5, sd = 5) # Independent variable
Y_i = beta_0 + beta_1*X_i + beta_2*X_i +U_i  # Dependent variable

# Formulate data.table
data_i = data.table(Y = Y_i, X = X_i)

# Run regressions
ols_i <- fixest::feols(data = data_i, Y ~ X)

# Extract slope coefficient and save
slope_DT_2[i] <- ols_i$coefficients[3]
slope_DT[i] <- ols_i$coefficients[2]
intercept_DT[i] <- ols_i$coefficients[1]

}

# Summary statistics
estimates_DT <- data.table(beta_2 = slope_DT_2,beta_1 = slope_DT, beta_0 = intercept_DT)

此代码没有为hp创建任何系数。我想知道如何将系数添加到模型中,然后预测结果并测试一辆车的mpg比另一辆低多少次。例如,马自达RX4的预测英里数比Datsun 710低多少倍。有什么好主意吗?谢谢你

就像我在评论中提到的,你应该使用两个独立的变量。此外,我想建议您使用lapply-函数,它使代码更短,因为您不需要初始化/存储部分。

estimates_DT <- do.call("rbind",lapply(1:M, function(i) {
# Generate data
U_i = rnorm(n, mean = 0, sd = 2) # Error
X_i_1 = rnorm(n, mean = 5, sd = 5) # First independent variable
X_i_2 = rnorm(n, mean = 5, sd = 5) #Second ndependent variable
Y_i = beta_0 + beta_1*X_i_1 + beta_2*X_i_2 + U_i  # Dependent variable
# Formulate data.table
data_i = data.table(Y = Y_i, X1 = X_i_1, X2 = X_i_2)

# Run regressions
ols_i <- fixest::feols(data = data_i, Y ~ X1 + X2)  
ols_i$coefficients
}))
estimates_DT <- setNames(data.table(estimates_DT),c("beta_0","beta_1","beta_2"))

为了比较两辆车的预测结果,定义以下函数,将要比较的两个车名作为参数:

compareCarEstimations <- function(carname1="Mazda RX4",carname2="Datsun 710") {
car1data <- mtcars[rownames(mtcars) == carname1,c("cyl","hp")]
car2data <- mtcars[rownames(mtcars) == carname2,c("cyl","hp")]

predsCar1 <- estimates_DT[["beta_0"]] + car1data$cyl*estimates_DT[["beta_1"]]+car1data$hp*estimates_DT[["beta_2"]]
predsCar2 <- estimates_DT[["beta_0"]] + car2data$cyl*estimates_DT[["beta_1"]]+car2data$hp*estimates_DT[["beta_2"]]

list(
car1LowerCar2 = sum(predsCar1 < predsCar2),
car2LowerCar1 = sum(predsCar1 >= predsCar2)
)
}

确保作为参数提供的名称是有效的名称,例如在rownames(mtcars)中。

相关内容

  • 没有找到相关文章

最新更新