在R中对嘈杂的1/x数据建模,从而获得“基本上完美的拟合".从摘要-为什么? [英] Modeling noisy 1/x data in R, getting "essentially perfect fit" from summary - why?
问题描述
使用以下玩具示例,尝试逐步介绍如何将倒数函数与数据拟合:
Just trying to walk myself through how fitting a reciprocal function to data would go, using the following toy example:
# includes
library(ggplot2)
library(forecast)
library(scales)
# make data
sampledata <- as.data.frame( .1 * seq(1, 20))
names(sampledata) <- c("index")
sampledata$truevalue <- (1/sampledata$index)
# make noisy data
sampledata$noise <- runif(20, .5, 1.5)
sampledata$noisyvalue <-sampledata$noise * (1/sampledata$index)
# linearize transformation
sampledata$invvalue <- 1/sampledata$noisyvalue
# linear model
samplemodel <- lm(sampledata$invvalue ~ sampledata$index)
# predict
sampledata$predictedValues_hat <- predict(samplemodel, newdata=as.data.frame(sampledata$index))
# de-transform
sampledata$predictedvalues <- 1/sampledata$predictedValues_hat
# plot
sampleplot <- ggplot(data = sampledata, aes(x = index, y = noisyvalue)) +
geom_point() +
geom_line(color = 'red', data = sampledata, aes(x = index, y = sampledata$truevalue)) +
ggtitle("1/x Modeling Example") +
theme(plot.title = element_text(color="black", size=14, face="bold", hjust = .5)) +
geom_line(color = 'blue', data = sampledata, aes(x = index, y = sampledata$predictedvalues)) +
scale_x_continuous(breaks=seq(0, 10))
show(sampleplot)
这似乎或多或少都可以,但是当我查看模型摘要时,我不了解发生了什么.每次运行,我都会得到相同的结果:
This seems to work more or less ok, but I'm not understanding what's happening when I look at the model summary. Every run, I get the same result:
> summary(model)
Call:
lm(formula = sampledata$invvalue ~ sampledata$index)
Residuals:
Min 1Q Median 3Q Max
-3.211e-16 -2.215e-16 -1.218e-16 1.251e-16 6.001e-16
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.641e-01 1.879e-16 3.535e+15 <2e-16 ***
sampledata$index 1.000e+00 3.176e-17 3.149e+16 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.331e-16 on 9 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: 1
F-statistic: 9.916e+32 on 1 and 9 DF, p-value: < 2.2e-16
Warning message:
In summary.lm(model) : essentially perfect fit: summary may be unreliable
>
此完全适合"消息的意义是什么?它肯定不会在模型上看起来像模型是完美的"-既不是嘈杂的数据,也不是真正的生成源.
What is the significance of this "perfect fit" message? It certainly doesn't look on the plot like the model is "perfect" - neither to the noisy data nor the true generating source.
推荐答案
我只是个笨蛋-评论者指出我在另一个模型上运行摘要.奥格.
I was just being stupid - the commenter pointed out I was running summary on a different model. Aargh.
这篇关于在R中对嘈杂的1/x数据建模,从而获得“基本上完美的拟合".从摘要-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!