[R] Passing formula and weights error

John Smith j@whct @end|ng |rom gm@||@com
Fri Aug 28 17:32:49 CEST 2020


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]]



More information about the R-help mailing list