title: Trouble Using Function which takes Formula Argument in R
body: <p>I have a function implementing the Fisher algorithm in R for a GLM which takes formula as an argument. However when attempting to run it I get the error: <code>Error in model.frame.default(formula = formula, drop.unused.levels = TRUE) :  invalid type (closure) for variable 't'</code></p>
  <p>I tried calling it in several ways (e.g using <code>as.formula(y~t)</code> but nothing seems to work.</p>
  <pre><code>myglm &lt;-  function(formula,data,start = 0) {
  
  X = model.matrix(formula,data) #It appears that the issue comes from this line
  Y = data[,1]
  n = dim(X)[1]
  p &lt;- dim(X)[2]
  beta_0 = rep(1,p)
  M = t(X)%*%X
  beta = rep(0,p)#Least Squares Estimate
  epsilon = 0.01
  
  #Run Fisher Iterations       
  while (norm(beta-beta_0,type = &quot;2&quot;)/norm(beta_0, type = &quot;2&quot;) &gt; epsilon) {
    
    beta_0 = beta
    eta = X %*% beta
    lambda = exp(eta)
    F =  t(X) %*% diag(as.vector(lambda)) %*% X #Fisher information matrix
    s = t(X) %*% (Y - exp(eta)) #Score function
    beta = beta + solve(F) %*% s
    
    
  }
  
  
  vcov = solve(F)
  coef = matrix(c(0,0,0,0),nrow = 2, ncol = 2)
  coef[,1] = beta
  coef[,2] = t(sqrt(diag(vcov)))
  
  colnames(coef) = c(&quot;Coefficients&quot;,&quot;Standard error&quot;)
  rownames(coef) = c(&quot;beta1&quot;, &quot;beta2&quot;)
  #Calculate Deviance
  mod_sat = glm(formula, family = poisson(link = &quot;log&quot;))
  log_likelihood = Y %*% eta - exp(eta)
  deviance = 2*(LogLik(mod_sat) - log_likelihood)
  
  
  return(list(coef,deviance,vcov))
    
    
  }
  
  
  f = formula(y ~ t)
  load(url(&quot;https://www.math.ntnu.no/emner/TMA4315/2020h/hoge-veluwe.Rdata&quot;)) #This is stored as &quot;data&quot;
  myglm(f, data)
  
  </code></pre>