为什么将拟合线映射到R中的xyplot时出错?

为什么将拟合线映射到R中的xyplot时出错?,r,plot,lattice,R,Plot,Lattice,尝试在使用lattice库的xyplot函数创建的绘图顶部绘制拟合值时遇到错误 Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet 我下面的教程使用with函数尝试映射通过线性混合模型估计的拟合值。我发现了一些关于此错误的讨论,因此我知道with语句没有找到由xyplot函数创建的绘图,但我不确定如何修复此问题 () 库(晶格) 图书馆(nlme) 研究2 libra

尝试在使用lattice库的xyplot函数创建的绘图顶部绘制拟合值时遇到错误

Error in plot.xy(xy.coords(x, y), type = type, ...) : 
  plot.new has not been called yet
我下面的教程使用with函数尝试映射通过线性混合模型估计的拟合值。我发现了一些关于此错误的讨论,因此我知道with语句没有找到由xyplot函数创建的绘图,但我不确定如何修复此问题

()

库(晶格)
图书馆(nlme)
研究2
library(lattice)
library(nlme)

study2 <- read.csv("https://stats.idre.ucla.edu/stat/data/study2.csv")
study2 <- within(study2, {
  id <- factor(id)
  exertype <- factor(exertype)
  diet <- factor(diet)
})

time.linear <- lme(pulse ~ exertype * time,
  random = list(id = pdDiag(~ time)), data = study2)
summary(time.linear)

fitted <- fitted(time.linear, level=0)

xyplot(pulse[exertype==1] ~ time[exertype==1], data = study2, groups = id,
  type = "o", ylim = c(50, 150), xlim = c(0, 800),
  panel = panel.superpose, col = "blue")

with(study2, lines(time[exertype==1], fitted[exertype==1],
  ylim = c(50, 150),  xlim = c(0, 800),
  type = "b", col = "dark blue", lwd = 4))