I'm wondering if this is a bug.
I have the following piece of code:
h2 <- hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))
model2 = nls(formula = log(counts[1:5]) ~a+log(mids[1:5])*gamma, start=list(gamma=-3,a=10),data=h2)
it breaks with the error:
Error in parse(text = x) : <text>:2:0: unexpected end of input
1: ~
^
But if I do:
h2 <- hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))
model2 = nls(formula = log(counts[1:5]) ~a+log(breaks[1:5])*gamma, start=list(gamma=-3,a=10),data=h2)
it doesn't give the error (it cannot fit this particular data, but can fit the data I really have).
The thing is, for the work I'm doing, I need the mid of the histogram intervals, not the breaks.
EDIT: After the error, traceback is:
7: parse(text = x)
6: eval(parse(text = x)[[1L]])
5: formula(eval(parse(text = x)[[1L]]))
4: formula.character(object, env = baseenv())
3: formula(object, env = baseenv())
2: as.formula(paste("~", paste(varNames[varIndex], collapse = "+")),
env = environment(formula))
1: nls(formula = log(counts[1:5]) ~ a + log(mids[1:5]) * gamma,
start = list(gamma = -3, a = 10), data = h2)
As I suspected:
d <- data.frame(counts = h2$counts[1:5],mids = h2$mids[1:5])
model2 = nls(formula = log(counts) ~a+log(mids)*gamma, start=list(gamma=-3,a=10),data=d)
runs without the formula parsing error (but of course still won't fit this small ill-formed data set).