如何将公式作为参数传递给r中的函数?

时间:2018-09-30 08:24:16

标签: r formula

如何在R中将公式作为参数传递?

以下代码适用于前两种情况,但是当我传递公式时,出现错误:Error in model.frame.default(formula = formula, weights = weights, na.action = na.omit, : invalid type (closure) for variable '(weights)'

makeModel<-function(formula,weights) {
    m <- lm(formula, na.action = na.omit, weights = weights)
    return(m);
}
run<-function(t) {
    f<-formula(t$y~t$x+t$r)
    m <- lm(t$y~t$x+t$r, na.action = na.omit, weights = t$size)
    m <- lm(f, na.action = na.omit, weights = t$size)
    m <- makeModels(f,t$size)    
}
l<-20
x<-seq(0,1,1/l)
y<-sqrt(x)
r=round(runif(n=length(x),min=0,max=.8))
n<-1:(l+1)
size=n/sum(n)
t<-data.frame(x,y,r,n,size)
run(t)

编辑1:此代码:

makeModel<-function(formula,weights,t) {
    print(class(weights))
    m <- lm(formula, na.action = na.omit, weights = weights,data=t)
    return(m);
}
run<-function(t) {
    f<-formula(y~x+r)
    f <- as.formula("t$y~t$x+t$r")
    m <- lm(y~x+r, na.action = na.omit, weights = t$size,data=t)
    m <- lm(f, na.action = na.omit, weights = t$size,data=t)
    m <- makeModel(f,t$size,t)    
}

产生:

model.frame.default中的错误(公式=公式,数据= t,权重=权重,:   变量'(weights)'的无效类型(关闭)

编辑2:作品:

makeModel <- function(formula, data) {
    # size is looked in data first, which is why this works
    m <- lm(formula, na.action = na.omit, weights = size, data =  data) # works
    #m <- lm(formula, na.action = na.omit, weights = data$size, data =  data) # fails!
    return(m)
}

r很奇怪!

有人知道为什么weights = data $ size行失败吗?

编辑3:得到了:weights = data $ size起作用了。

makeModel<-function(formula,w,data) {
    print(class(weights))
    m <- lm(formula, na.action = na.omit, weights = size, data =  data) # works
    m <- lm(formula, na.action = na.omit, weights = data$size, data =  data) #works
    m <- lm(formula, na.action = na.omit, weights = w,data=data) # fails
    return(m);
}
run<-function(data) {
    f<-formula(y~x+r)
    #f <- as.formula("t$y~t$x+t$r")
    m <- lm(y~x+r, na.action = na.omit, weights = data$size,data=data)
    m <- lm(f, na.action = na.omit, weights = data$size,data=data)
    m <- makeModel(f,data$size,data)    
}

最后一个失败,并显示以下错误:eval(扩展名,数据,环境)中的错误:找不到对象“ w”

2 个答案:

答案 0 :(得分:0)

避免分配一个名为t的对象,该对象与转置函数一致。看回溯产量

makeModel<-function(formula,weights) {
  m <- lm(formula, na.action = na.omit, weights = weights)
  return(m)
}
run<-function(x) {
  f<-formula(x$y~x$x+x$r)
  m <- lm(x$y~x$x+x$r, na.action = na.omit, weights = x$size)
  m <- lm(f, na.action = na.omit, weights = x$size)
  m <- makeModel(f,x$size)    
}
l<-20
x<-seq(0,1,1/l)
y<-sqrt(x)
r=round(runif(n=length(x),min=0,max=.8))
n<-1:(l+1)
size=n/sum(n)
x<-data.frame(x,y,r,n,size)
run(x)
#R Error in model.frame.default(formula = formula, weights = weights, na.action = na.omit,  : 
#R    invalid type (closure) for variable '(weights)'
traceback()
#R 7: model.frame.default(formula = formula, weights = weights, na.action = na.omit, 
#R                        drop.unused.levels = TRUE)
#R 6: stats::model.frame(formula = formula, weights = weights, na.action = na.omit, 
#R                       drop.unused.levels = TRUE)
#R 5: eval(mf, parent.frame())
#R 4: eval(mf, parent.frame())
#R 3: lm(formula, na.action = na.omit, weights = weights) at #3
#R 2: makeModel(f, x$size) at #5
#R 1: run(t)

现在debug(model.frame.default)表明this line是由于these linethis line而出错的地方。原因是它调用

eval(list(weights = weights), environment(formula), environment(formula))

,并且在weights环境(分配了公式的环境)中没有分配run对象,因此它产生stats::weights。三种解决方案是

makeModel <- function(formula, weights) {
  environment(formula) <- environment()
  lm(formula, na.action = na.omit, weights = weights)
}
run<-function(x) {
  f <- x$y ~ x$x + x$r
  makeModel(f, x$size)  
}
x1 <- run(x)

makeModel <- function(formula, weights) {
  cl <- match.call()
  cl[[1L]] <- quote(lm)
  cl$na.action <- quote(na.omit)
  eval(cl, parent.frame())
}
run<-function(x) {
  f <- x$y ~ x$x + x$r
  makeModel(f, x$size)  
}
x2 <- run(x)

makeModel <- function(formula, weights, x) {
  cl <- match.call()
  cl[[1]] <- quote(lm)
  cl$x <- NULL
  cl[c("data", "formula", "na.action")] <- 
    list(quote(x), formula, quote(na.omit))
  eval(cl)
}
run<-function(x) {
  f <- y ~ x + r
  makeModel(f, size, x)  
}
x3 <- run(x)

stopifnot(all.equal(coef(x1), coef(x2)))
stopifnot(all.equal(coef(x1), coef(x3), check.attributes = FALSE))

例如,上面的第一个解决方案暗示

eval(list(weights = weights), environment(formula), environment(formula))

成功,因为在weights的环境中分配了一个formula对象。第二种解决方案是在run环境中使用weights = x$size进行呼叫,从而成功了。尽管您知道weights自变量始终是size列,但第三个问题与Roman Luštrik's answer相似,尽管他的解决方案比我建议的第三个要干净得多。这里的电话是

eval(list(weights = size), data, environment(formula))

之所以有效,是因为sizedata中的一列。

答案 1 :(得分:0)

请参阅?as.formula中的示例。您不应从变量名中显式调用变量。公式应该是抽象的,lm将知道应从data中提取哪些变量,您应该指定这些变量。

makeModels <- function(formula, data) {
  # size is looked in data first, which is why this works
  m <- lm(formula, na.action = na.omit, weights = size, data =  data)
  return(m)
}

run <- function(t) {
  f <- formula(y ~ x + r)
  m1 <- lm(formula = f, na.action = na.omit, weights = size, data = t)
  m2 <- makeModels(formula = f, data = t)
  return(list(m1, m2))
}

l<-20
x<-seq(0,1,1/l)
y<-sqrt(x)
r=round(runif(n = length(x), min = 0, max = 0.8))
n<-1:(l+1)
size=n/sum(n)
t<-data.frame(x,y,r,n,size)
run(t)

[[1]]

Call:
lm(formula = f, data = t, weights = t$size, na.action = na.omit)

Coefficients:
(Intercept)            x            r  
   0.327154     0.706553    -0.008167  


[[2]]

Call:
lm(formula = formula, data = data, weights = size, na.action = na.omit)

Coefficients:
(Intercept)            x            r  
   0.327154     0.706553    -0.008167