Furthermore, we know from the regression line that the expected error is 0.
Here is the code to implement this idea in R. You can get the data to work on in the bottom of the page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data <- read.csv("data.csv", header = T) | |
attach(data) | |
plot(Y~X, col = "blue") | |
# Generate a linear model and plot it | |
model <- lm(Y ~ X) | |
abline(model, lwd=2, col="red") | |
# Calculate the residuals | |
resid <- signif(residuals(model), 5) | |
# and the predicted values | |
predicted_values <- predict(model) | |
# Set the bounds | |
bounds <- seq(from = min(resid), to = max(resid), by = 0.2) | |
# Check the relative frequencies and error distribution | |
error_distribution <- table(cut(resid, bounds))/sum(table(cut(resid,bounds))) | |
# Plot error distribution | |
plot(error_distribution, main = "Frequencies") | |
# From the plot, we can assume, that the error should be approximately iid. | |
# Therefore runif() could work as an error simulator | |
# We now generate 10 errors | |
simulated_errors <- runif(length(predicted_values), min = min(resid), max = max(resid)) | |
simulated_values <- c() | |
# We simulate actual values of Y by adding errors to the predicted values | |
for(i in 1:length(X)) | |
{ | |
simulated_values[i] <- (predicted_values[i] + simulated_errors[i]) | |
} | |
# Here are our simulated_values | |
simulated_values | |
# We plot again the data and the model, then we add our simulated values | |
plot(Y~X, col = "blue") | |
abline(model, lwd=2, col="red") | |
points(X, simulated_values, type = "p",col="red") | |
detach(data) |
The result should look something like this: In blue the actual data and in red the simulated one.
Hope this was useful, if you know the name of this method, please leave a comment and let me know. Click here to get the data I used.
This comment has been removed by the author.
ReplyDelete