We define two functions f1
and f2
that must return the same thing:
library(nlme)
data(Oxide)
library(varComp)
varComp(Thickness~Source, Oxide, ~Lot/Wafer)
f1 <- function(dat){
data <- dat
varComp(Thickness~Source, data, ~Lot/Wafer)
}
f2 <- function(dat){
varComp(Thickness~Source, dat, ~Lot/Wafer)
}
R-3.3.3
> f1(Oxide)
Variance component model fit
Call:
varComp(fixed = Thickness ~ Source, data = data, random = ~Lot/Wafer)
Fixed effect estimates:
(Intercept) Source2
1995.11111 10.08333
Variance component estimates:
Lot Lot:Wafer error
119.89249 35.86574 12.56944
Number of observations: 72
> f2(Oxide) # error
Error in is.data.frame(data) : object 'dat' not found
How is this error possible when it works f1(Oxide)
?
R-3.4.2
> f1(Oxide)
Error in eval(predvars, data, env) : object 'Lot' not found
Called from: eval(predvars, data, env)
Browse[1]> f2(Oxide)
Error during wrapup: object 'Lot' not found
Why are there two different errors? And why is the difference compared to R-3.3.3? Any guess?
There varComp
is a programming error in, and I would like to understand, because I have a script that works in R-3.3.3, but not in R-3.4.2.
Edit
Now I get different error messages with R-3.4.2:
> f1(Oxide)
Error in as.data.frame.default(data, optional = TRUE) :
cannot coerce class ""function"" to a data.frame
> f2(Oxide)
Error in is.data.frame(data) : object 'dat' not found
This works if assigned dat
not locally:
> f3 <- function(dat){
+ dat <<- dat
+ varComp(Thickness~Source, dat, ~Lot/Wafer)
+ }
> f3(Oxide)
Variance component model fit
Call:
varComp(fixed = Thickness ~ Source, data = dat, random = ~Lot/Wafer)
......