histogram $ breaks适用于数据,直方图$ mids给出错误

时间:2013-01-16 18:54:15

标签: r histogram curve-fitting least-squares

我想知道这是不是一个错误。

我有以下代码:

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)

它打破了错误:

Error in parse(text = x) : <text>:2:0: unexpected end of input
1: ~
  ^

但如果我这样做:

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)

它没有给出错误(它不适合这个特定的数据,但可以适合我真正拥有的数据)。

问题是,对于我正在做的工作,我需要直方图间隔的中间,而不是休息。

编辑: 出错后,追溯是:

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)

1 个答案:

答案 0 :(得分:2)

我怀疑:

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)

在没有公式解析错误的情况下运行(但当然仍然不适合这个小的不正确的数据集)。