如何在 xyplot / panel 函数中使用预测?

How to use predict within xyplot / panel function?

我通过

绘制和拟合数据
> xyplot(Amplification~Voltage,
        data = before_database[before_database$Serial_number==912009913,],
        grid = TRUE, 
        main = "SN = 912009913", 
        panel = function(x, y) {
            panel.xyplot(x, y) 
            fit<-lm(y ~ poly(x,2))
            panel.lines(x, fitted(fit), col.line="black")
        })

before_database 是我的数据框 ( before_database ),通过 Serial_number 我选择了数据框的分组子集。因此,放大反对电压。

现在我有两个问题:如何从 lm 拟合中获取相关值?目前我不太确定我需要哪个,所以我一般都会问。 而且,另外,如何在拟合中的某个点获得相应的值?例如。我需要放大时的电压 = 150(没有数据点)。 Fitted() 应该这样做,但我不适应它..

剧情如下:

谢谢!

PS:我知道情节不代表数据:)这终于是我找到最佳情节的任务了。

编辑:根据讨论,我想首先在 xyplot 之外拟合模型,因为我已经习惯了。因此我现在有:

before_database.frame<- read.table("APD_data.txt", 
                                  header = TRUE,
                                  sep = "",
                                  dec="."
                                 )

test.frame<- before_database.frame[before_database.frame$Serial_number==912009913, ]

test.fit<- lm(Amplification ~ poly(Voltage,2), data=test.frame)

xyplot(
      Amplification ~ Voltage,
      data = test.frame,
      grid = TRUE,
      main = "SN = 91200913",
      panel = function(x,y)
      {
          panel.xyplot(x,y)
          panel.lines(x, fitted(test.fit),
          col.line="black")
      }
      )

test.fit$coefficients[1]

new.df<- data.frame(Amplification=150)
predict(test.fit, new.df)

申请时 "predict" 我收到:

"Error in poly(Voltage, 2, coefs = list(alpha = c(247.536114864865, 174.327996600877 : object 'Voltage' not found"

我猜框架"new.df"需要可变电压?但是我如何在没有价值的情况下提供它?

要在预测变量的新值处预测您的响应变量,请使用 predict 函数而不是 fitted 函数。你给它你的模型和一个新的数据框,它有你想要预测的预测变量 (x) 的值。

不确定您还想提取什么或要用它做什么。 运行 命令:

methods(class='lm')

将向您展示可用于 lm 对象结果的函数,浏览帮助文件(然后深入阅读有趣的文件)可能会帮助您找到所需的信息。