简体   繁体   English

如何使用stepAIC进行仿真研究

[英]how to conduct a simulation study using stepAIC

I have to code a simulation study in R. I have X1,...,X15~N(0,1) explanatory variables and Y~N(2+2*X1+0.8*X2-1.2*X15, 1) and I need to simulate n=100 values and repeat that for iter=100 times. 我必须在R中编写一个模拟研究代码。我有X1,...,X15〜N(0,1)解释变量和Y〜N(2 + 2 * X1 + 0.8 * X2-1.2 * X15,1)和我需要模拟n = 100的值,并重复iter = 100次。 Then, for each linear model created I have to find the best sub-model, using stepAIC. 然后,对于每个创建的线性模型,我都必须使用stepAIC找到最佳子模型。 I wrote the following code: 我写了以下代码:

set.seed(1234)
sim <- function (sd) {
n <- 100
p <- 15
X <- matrix(rnorm(n * p), n, p)
mu <- 2 + 2*X[,1] + 0.8*X[,2] - 1.2*X[,15]
Y <- matrix(rnorm(100, mu,sd))
sim<-data.frame(Y,X)
r<- lm(Y~., data = sim)
library(MASS)
r0<-lm(Y~1, data=sim)
res<-stepAIC(r0,k=2,direction="forward", scope=list(lower=~1, upper=r))
return(res$coefficients)
}

sim(1)
oo1<- lapply(1:100, sim)

As I am an inexperienced R-user, I think that I'm doing something wrong. 因为我是经验不足的R用户,所以我认为自己做错了。 The purpose of the study is to find if within the 100 best sub-models (according to stepAIC), there are models that can find the real one (Y=2+2*X1+0.8*X2-1.2*X15+e). 该研究的目的是查找在100个最佳子模型中(根据stepAIC)是否存在可以找到真实模型的模型(Y = 2 + 2 * X1 + 0.8 * X2-1.2 * X15 + e) 。 In case, I'm doing the wrong things could I get some help/hints so as to implement it correctly? 万一我做错了什么,我可以得到一些帮助/提示,​​以正确实施它吗?

Here is a working version of your code: 这是您的代码的有效版本:

library(MASS)  
set.seed(1234)

sim <- function(sd, n, p) {
  X <- matrix(rnorm(n * p), n, p)
  mu <- 2 + 2*X[,1] + 0.8*X[,2] - 1.2*X[,p]
  Y <- rnorm(n, mean=mu, sd=rep(sd,n))
  df <- data.frame(Y,X)
  r <- lm(Y~., data=df)
  r0 <- lm(Y~1, data=df)
  res <- stepAIC(r0, k=2, direction="forward", 
           scope=list(lower=~1, upper=r), trace=F)
  return(res$coefficients)
}

n <- 100
p <- 15
sim(1, n, p)
oo1 <- lapply(1:100, sim, n, p)

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

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