is.data.frame(data)出错:找不到对象'data =''

时间:2017-09-15 21:17:24

标签: r function parameter-passing environment gam

我很擅长编写一个函数:我正在努力创建一个适合GAM的函数。我找到了一个线性模型的例子,它完美地运行。但是,尝试对GAM使用相同的结构会引发错误。

线性模型:

@using ProjectName.Controllers;
@{
    AnalysisController AC = new AnalysisController();
    ViewBag.Title = "Index";
}

@foreach(var od in ProductPop())
{
   <table>
      <tr>
          <th>
              Product Name
          </th>
          <th>
             Quantity
          </th>
      </tr>
       <tr>
           <td>
               @od.ProductName
           </td>
           <td>
               @od.Quantity
           </td>
       </tr>
   </table>
}

和GAM版本

library(mgcv)
library(ggplot2)
theme_set(theme_bw())
set.seed(100)
dd <- data.frame(x=1:100,y=round(rnorm(100,mean=100),1), z=round(runif(100,1,4),1))
lm(y~x, weights=z, data=dd)$call
lm(formula = y ~ x, data = dd, weights = z)

f2 <- function(f,w,d){
   do.call("lm", list(formula=as.formula(f), weights=as.name(w), data=as.name(d)))
 }
 f2("y~x", "z", "dd")$call
lm(formula = y ~ x, data = dd, weights = z)

1 个答案:

答案 0 :(得分:0)

呼叫:

f2 <- function(f, m, s, d){
    do.call("gam", list(formula=as.formula(f),method = "REML",select = s, data = as.name(d)))
}
f2("y ~ s(x, k=64, bs='ad')", "REML", TRUE, d = "dd")$call

结果:

gam(formula = y ~ s(x, k = 64, bs = "ad"), data = dd, method = "REML", 
    select = TRUE)