r - Invalid type (closure) for a variable that is not a function -
loess.smooth <- function(dat) { dat <- dat[complete.cases(dat),] ## response vars <- colnames(dat) ## covariate id <- 1:nrow(dat) ## define loess filter function (fitting loess regression line) loess.filter <- function (x, span) loess(formula = paste(x, "id", sep = "~"), data = dat, degree = 1, span = span)$fitted ## apply filter column-by-column new.dat <- as.data.frame(lapply(vars, loess.filter, span = 0.75), col.names = colnames(dat)) } when try apply loess.smooth dataframe, error:
error in model.frame.default(formula = paste(x, "id", sep = "~"), data = dat) : invalid type (closure) variable 'id' i don't understand why problem since id not function, implied error. when run through these lines of code outside of function, works fine , want do.
it scoping issue involving passing vector of strings loess function instead of passing vector of formulas. problem environment returns null former, loess doesn't know find it. if wrap formula in as.formula works. variable assigned local environment inside function call default.
as cryptic error, happens when name variable same name of given function package loaded, since if function doesn't find variable in local environment, scope in loaded packages function. in case, id function loaded dplyr library.
Comments
Post a Comment