如何添加适合格子的nls()

时间:2014-04-03 18:06:30

标签: r lattice nls

我已经开始从基础R图形切换到图形图形。我现在有一些数据和nls()适合该数据,我正在尝试将两者都绘制在一个图表上。所以,作为一个简单的基础R图形代码:

require(lattice)
# Data
V <- c(45.5513,57.9145,66.3092,72.3338,79.7007,92.5738,107.9735,153.7357,210.8857)
E <- c(-0.05403,-0.20890,-0.24562,-0.25644,-0.26019,-0.25213,-0.23181,-0.17106,-0.10860)

# Guesses
V0g <- 118.5614
E0g <- -0.2579124
B0g <- 0.004868107
Bp0g <- 4

# nls fit
birchfit <- nls(E ~ E0 + (9*V0*B0)/16 * ( ((V0/V)^(2/3)-1)^3*Bp0 + ((V0/V)^(2/3)-1)^2 * (6-4*(V0/V)^(2/3))  ),start=list(B0=B0g,V0=V0g,Bp0=Bp0g,E0=E0g))

# Plot
VV <- seq(V[1],tail(V,1),length=1000)
plot(V,E)
lines(VV,predict(birchfit,data.frame(V=VV)))

现在对于格子变体,我试试这个,

VV <- seq(V[1],tail(V,1),length=1000)
print(xyplot(E ~ V,
     panel=panel.xyplot(VV,predict(birchfit,data.frame(V=VV)),type="l")))

然后我被告知(通过图表中心的文字)

Error using packet 1
'what' must be a character string or function

1 个答案:

答案 0 :(得分:0)

通过使用来自latticeExtra包的as.layer将两个单独的图形添加到同一个面板,我能够实现我想要的。也许有更好的解决方案,但这对我有用。

require(latticeExtra)
...
VV <- seq(V[1],tail(V,1),length=1000)
plot1 <- xyplot(E ~ V)
plot2 <- xyplot(predict(birchfit,data.frame(V=VV)) ~ VV,type="l")
print(plot1 + as.layer(plot2))