问题
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.
回答1:
I was just being stupid - the commenter pointed out I was running summary on a different model. Aargh.
来源:https://stackoverflow.com/questions/57237684/modeling-noisy-1-x-data-in-r-getting-essentially-perfect-fit-from-summary-w