代码之家  ›  专栏  ›  技术社区  ›  Luis

在R中为全局环境分配一个动态函数

  •  0
  • Luis  · 技术社区  · 8 年前

    这个问题对于这个论坛的一些成员来说是一个简单的问题,但是我花了很多时间寻找答案,我没有找到任何有效的答案。 让我澄清一下:我有一个简单的函数,它执行线性回归,结果变成一个变量,这个变量被分配到全局环境中。简单的方法。

    但是,每次运行这个函数时,变量都会被替换,这是我不想要的。 我只想定义一个特定的名称,而不是替换这个变量。

    请查看此代码以了解我的问题:

    set.seed(123)
    ds<-data.frame(收入=rnorm(1000700,20),性别=c(0,1),年龄=rnorm(1000,30,10))
    
    回归函数(iv_字符串){
    回归公式<-as.formula(paste(“income~”,iv_string))。
    结果<<-lm(回归公式,ds)
    绘图(结果)
    打印(结果)
    }
    回归(“年龄”)。
    回归(“性别”)
    

    像那样的

    regress<-函数(iv_string,**custom_name**){
    回归公式<-as.formula(paste(“income~”,iv_string))。
    结果[**自定义_名称**]<<-lm(回归_公式,ds)
    绘图(结果)
    打印(结果)
    }
    回归分析(“性别”,“性别收入”)一旦在全球环境中分配,该模型将用于其他分析。
    回归(“年龄”,“收入”上的年龄”)将使用此模型!你看!
    

    多谢。

    但是,每次运行这个函数时,变量都会被替换,这是我不想要的。 我只想定义一个特定的名称,而不是替换这个变量。

    请查看此代码以了解我的问题:

    set.seed(123)
    ds <- data.frame(income=rnorm(1000,700,20), sex=c(0,1), age=rnorm(1000,30,10))
    
    regress <- function(iv_string) {
      regression_formula <- as.formula(paste("income ~", iv_string))
      results <<- lm(regression_formula, ds)
      plot(results)
      print(results)
    }
    regress("age")
    regress("sex")
    

    像那样的

      regress <- function(iv_string, **custom_name**) {
          regression_formula <- as.formula(paste("income ~", iv_string))
          results[**custom_name**] <<- lm(regression_formula, ds)
          plot(results)
          print(results)
        }
    regress("sex","sex_on_income") #this model will be used in other analysis, once it will be assigned at global environment
    regress("age","age_on_income") #this model will be used !!
    

    final solution 非常感谢。

    2 回复  |  直到 8 年前
        1
  •  2
  •   G. Grothendieck    8 年前

    lm env 它允许iv_字符串引用 ds iv_string 那我们就用 fo <- paste("income ~", iv_string) do.call(...) 以确保 输出显示实际公式,而不仅仅是 fo 环境 parent.frame() .GlobalEnv globalenv()

    regress <- function(iv_string, custom_name = iv_string, env = parent.frame()) {
      fo <- as.formula(paste("income ~", iv_string), env)
      env[[custom_name]] <- results <- do.call("lm", list(fo, quote(ds)))
      plot(results)
      print(results)
      invisible(results)
    }
    
    regress("age", "lm_age")
    

    Call:
    lm(formula = income ~ age, data = ds)  <-- note that formula shows, not just fo
    
    Coefficients:
    (Intercept)          age  
       695.1540       0.1699  
    

    也就是说,R中的常规样式不是将对象写出到父帧或全局环境,而是返回它们:

    regress <- function(iv_string, env = parent.frame()) {
      fo <- as.formula(paste("income ~", iv_string), env)
      results <- do.call("lm", list(fo, quote(ds)))
      plot(results)
      print(results)
      invisible(results)
    }
    
    lm_age <- regress("age")
    
        2
  •  1
  •   Dan    8 年前

    regress <- function(iv_string, custom_name) {
      regression_formula <- as.formula(paste("income ~", iv_string))
      assign(custom_name, lm(regression_formula, ds), envir = .GlobalEnv)
      plot(get(custom_name))
      print(get(custom_name))
    }
    
    regress("age", "age_reg")
    

    在上面的代码中,我 assign custom_name .对于 plot print get age_reg

    推荐文章