By anspiess
When simulating linear models based on some defined slope/intercept and added gaussian noise, the parameter estimates vary after least-squares fitting. Here is some code I developed that does a double transform of these models as to obtain a fitted model with EXACT defined parameter estimates a
(intercept) and b
(slope).
It does so by:
1) Fitting a linear model #1 to the x,y
data.
2) Correcting y
by Image may be NSFW.
Clik here to view.: Image may be NSFW.
Clik here to view..
3) Refitting linear model #2: Image may be NSFW.
Clik here to view..
4) Correcting y
by Image may be NSFW.
Clik here to view.: Image may be NSFW.
Clik here to view..
5) Refitting linear model #3: Image may be NSFW.
Clik here to view., which is the final model with parameter estimates
a
and b
.
Below is the code:
exactLM x = 1:100, ## predictor values
b = 0.01, ## slope
a = 3, ## intercept
error = NULL, ## homoscedastic error
n = 1, ## number of replicates
weights = NULL, ## possible weights, i.e. when heteroscedastic
plot = TRUE, ## plot data and regression
... ## other parameters to 'lm'
)
{
if (is.null(error)) stop("Please ...read more
Source:: r-bloggers.com