与在R中创建NLS模型有关的问题
我愿意根据功能固化〜A * Atan(B * Time)
插入两条曲线,并拟合以下代码中报告的数据。我遇到两个问题:
library(tidyverse)
library(investr)
library(ggplot2)
#DATAFRAME
RawData <- data.frame("Time" = c(0, 4, 8, 24, 28, 32, 0, 4, 8, 24, 28, 32), "Curing" = c(0, 28.57, 56.19, 86.67, 89.52, 91.42, 0, 85.71, 93.33, 94.28, 97.62, 98.09), "Grade" = c("Product A", "Product A", "Product A", "Product A", "Product A", "Product A", "Product B", "Product B", "Product B", "Product B", "Product B", "Product B"))
attach(RawData)
model <- nls(Curing ~ a * atan(b * Time), data= RawData, control=nls.control(printEval=TRUE, minFactor=2^-24, warnOnly=TRUE))
new.data <- data.frame(time=seq(1, 32, by = 0.1))
interval <- as_tibble(predFit(model, newdata = new.data, interval = "confidence", level= 0.9)) %>% mutate(Time = RawData$Time)
第一个是输入最后一行后立即错误: 分配中的错误(Xname,newdata [,xName]):第一个参数无效
我试图在没有成功的情况下更改new.data
的值。如果我删除了可选的参数newdata =
我可以合适,但是看起来拟合是在插值的,而无需区分两个系列的整个数据集。
命令行的下方以获取图形:
Graph <- ggplot(data=RawData, aes(x=`Time`, y=`Curing`, col=Grade)) + geom_point(aes(color = Grade), shape = 1, size = 2.5)
Graph + geom_line(data=interval, aes(x = Time, y = fit))+
geom_ribbon(data=interval, aes(x=Time, ymin=lwr, ymax=upr), alpha=0.5, inherit.aes=F, fill="blue")+
theme_classic()
是否可以同时拥有:平滑而串联分开的拟合?
I am willing to interpolate two curves, according to the function Curing ~ a * atan(b * Time)
, fitting the data reported in the code below. I am getting two problems with this:
library(tidyverse)
library(investr)
library(ggplot2)
#DATAFRAME
RawData <- data.frame("Time" = c(0, 4, 8, 24, 28, 32, 0, 4, 8, 24, 28, 32), "Curing" = c(0, 28.57, 56.19, 86.67, 89.52, 91.42, 0, 85.71, 93.33, 94.28, 97.62, 98.09), "Grade" = c("Product A", "Product A", "Product A", "Product A", "Product A", "Product A", "Product B", "Product B", "Product B", "Product B", "Product B", "Product B"))
attach(RawData)
model <- nls(Curing ~ a * atan(b * Time), data= RawData, control=nls.control(printEval=TRUE, minFactor=2^-24, warnOnly=TRUE))
new.data <- data.frame(time=seq(1, 32, by = 0.1))
interval <- as_tibble(predFit(model, newdata = new.data, interval = "confidence", level= 0.9)) %>% mutate(Time = RawData$Time)
The first is an error as soon as I input the last line:Error in assign(xname, newdata[, xname]) : first argument not valid
I have tried to change the values of new.data
without success. If I remove the optional argument newdata =
I can fit, but it looks like the fitting is made interpolating the whole set of data without differentiating the two series.
Below the command lines for getting the graph:
Graph <- ggplot(data=RawData, aes(x=`Time`, y=`Curing`, col=Grade)) + geom_point(aes(color = Grade), shape = 1, size = 2.5)
Graph + geom_line(data=interval, aes(x = Time, y = fit))+
geom_ribbon(data=interval, aes(x=Time, ymin=lwr, ymax=upr), alpha=0.5, inherit.aes=F, fill="blue")+
theme_classic()
Is it possible to have both: a smooth and series-separated fitting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的错误是由错别字(
time
而不是time
innew.data
)引起的。但是,这将无法解决为每个系列获得一个色带的问题。为了一次性,您将需要两个不同的模型来为两组不同的数据集。最好使用拆分式插入式习语来创建一个单个预测数据框架。它还有助于绘制是否具有
等级
列,并且fit
列将其重命名为curing
,然后允许绘图非常简单:
< a href =“ https://i.sstatic.net/tudnz.png” rel =“ nofollow noreferrer”>
一般方法
我认为这是一种非常有用的技术,并且可能引起更广泛的兴趣,因此,如果人们希望使用<<<代码> GEOM_SMOOTH 将是围绕NLS创建小包装器,
prepfit
:这允许使用
ggplot
:预测和置信带,我们都可以做:
Your error is caused by a typo (
time
instead ofTime
innew.data
). However, this will not fix the problem of getting one ribbon for each series.To do this as a one-off, you will need two separate models for the two different sets of data. It is best to use the split-apply-bind idiom to create a single prediction data frame. It also helps plotting if this has a
Grade
column and thefit
column is renamed toCuring
This then allows the plot to be quite straightforward:
General approach
I think this is quite a useful technique, and might be of broader interest, so a more general solution if one wishes to plot confidence bands with an nls model using
geom_smooth
would be to create little wrappers around nls andpredFit
:This allows very simple plotting with
ggplot
:To put both prediction and confidence bands, we can do: