R Step函数在全局环境中查找数据,而不是在定义的函数内部查找数据 [英] R Step function looks for data in global environment, not inside defined function

查看:101
本文介绍了R Step函数在全局环境中查找数据,而不是在定义的函数内部查找数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对逐步回归有问题,我的理解是我没有正确传递参数 Data .

I have a problem with step forward regression and My understanding is that i don't pass argument Data correctly.

我具有以下功能:

ForwardStep <- function(df,yName, Xs, XsMin) {
    Data <- df[, c(yName,Xs)]
    fit <- glm(formula = paste(yName, " ~ ", paste0(XsMin, collapse = " + ")),
               data = Data, family = binomial(link = "logit") )
    ScopeFormula <- list(lower = paste(yName, " ~ ", paste0(XsMin, collapse = " + ")), 
                         upper = paste(yName, " ~ ", paste0(Xs, collapse = " + ")))
    result <- step(fit, direction = "forward", scope = ScopeFormula, trace = 1 )

    return(result)
}

当我尝试使用以下参数运行它

When I try to run it with following arguments

df <- data.frame(Y= rep(c(0,1),25),time = rpois(50,2), x1 = rnorm(50, 0,1),
                 x2 = rnorm(50,.5,2), x3 = rnorm(50,0,1))
yName = "Y"
Xs <- c("x1","x2","x3")
XsMin <- 1

res <- ForwardStep(df,Yname,Xs,XsMin)

我收到一个错误: is.data.frame(data)中的错误:找不到对象'Data'

但是,如果我先在Global Env中定义 Data ,它就可以很好地工作.

But if I first define Data in Global Env it works perfectly fine.

Data <- df[, c(yName,Xs)]

res <- ForwardStep(df,Yname,Xs,XsMin)

我猜我对功能步骤的实现错误,但是我不完全知道如何正确地执行它.

I guess that I have wrong implementation of function step however I don't exactly know how to do it the right way.

推荐答案

您需要认识到公式始终具有关联的环境,请参见 help("formula").永远不要将文本传递给模型函数的 formula 参数.如果这样做,迟早会遇到范围界定问题.通常,我建议改为使用该语言进行计算,但是您也可以在正确的范围内根据文本创建公式:

You need to realize that formulas always have an associated environment, see help("formula"). One should never pass text to the formula parameter of model functions, never ever. If you do that, you will encounter scoping issues sooner or later. Usually, I'd recommend computing on the language instead, but you can also create the formulas from text in the correct scope:

ForwardStep <- function(df,Yname, Xs, XsMin) {
  Data <- df[, c(Yname,Xs)]
  f1 <- as.formula(paste(Yname, " ~ ", paste0(XsMin, collapse = " + ")))

  fit <- glm(formula = f1,
             data = Data, family = binomial(link = "logit") )
  f2 <- as.formula(paste(Yname, " ~ ", paste0(XsMin, collapse = " + ")))
  f3 <- as.formula(paste(Yname, " ~ ", paste0(Xs, collapse = " + ")))

  ScopeFormula <- list(lower = f2, 
                       upper = f3)
   step(fit, direction = "forward", scope = ScopeFormula, trace = 1)
}

df <- data.frame(Y= rep(c(0,1),25),time = rpois(50,2), x1 = rnorm(50, 0,1),
                 x2 = rnorm(50,.5,2), x3 = rnorm(50,0,1))
YName = "Y"
Xs <- c("x1","x2","x3")
XsMin <- 1

res <- ForwardStep(df,YName,Xs,XsMin)
#Start:  AIC=71.31
#Y ~ 1
#
#       Df Deviance    AIC
#<none>      69.315 71.315
#+ x1    1   68.661 72.661
#+ x3    1   68.797 72.797
#+ x2    1   69.277 73.277

(公共服务公告:逐步回归是垃圾生成器.有更好的统计技术可供使用.)

(Public service announcement: step-wise regression is a garbage generator. There are better statistical techniques available.)

这篇关于R Step函数在全局环境中查找数据,而不是在定义的函数内部查找数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆