[R] Passing formula and weights error

John Smith
2020-08-28T15:32:49Z

Dear R-help:

I am writing a function based on glm and would like some variations of
weights. In the code below, I couldn't understand why the second glm
function fails and don't know how to fix it:

Error in eval(extras, data, env) : object 'newweights' not found
 Calls: print ... eval -> <Anonymous> -> model.frame.default ->
eval -> eval
 Execution halted

### R code
y <- rnorm(100)
 x <- rnorm(100)
 data <- data.frame(cbind(x, y))
 weights <- rep(1, 100)
 n <- 100
 myglm <- function(formula, data, weights){
     ## this works
     print(glm(formula, data, family=gaussian(), weights))
     ## this is not working
     newweights <- rep(1, n)
     glm(formula, data, family=gaussian(), weights=newweights)
 }
 myglm(y~., data, weights)

	[[alternative HTML version deleted]]