[R] how to get perfect fit of lm if response is constant

Peter Ehlers ehlers at ucalgary.ca
Fri Jan 8 19:43:59 CET 2010


You need to review the assumptions of linear models:
y is assumed to be the realization of a random variable,
not a constant (or, more precisely: there are assumed to
be deviations that are N(0, sigma^2).

If you 'know' that y is a constant, then you have
two options:

1. don't do the regression because it makes no sense;
2. if you want to test lm()'s handling of the data:

fm <- lm(y ~ x, data = df, offset = rep(1, nrow(df)))

(or use: offset = y)

  -Peter Ehlers

Jan-Henrik Pötter wrote:
> Hello.
> 
> Consider the response-variable of data.frame df is constant, so analytically
> perfect fit of a linear model is expected. Fitting a regression line using
> lm result in residuals, slope and std.errors not exactly zero, which is
> acceptable in some way, but errorneous. But if you use summary.lm it shows
> inacceptable error propagation in the calculation of the t value and the
> corresponding p-value for the slope, as well R-Square – just consider the
> adj R-Square of 0.6788! This result is independent of which mode used for
> the input vectors. Is there any way to get the perfect fitted regression
> curve using lm and prevent this error propagation? I consider rounding all
> values of the lm-object afterwards to somewhat precision as a bad idea.
> Unfortunately there is no option in lm for calculation precision. 
> 
>  
> 
>> df<-data.frame(x=1:10,y=1)
> 
>> myl<-lm(y~x,data=df)
> 
>  
> 
>> myl
> 
>  
> 
> Call:
> 
> lm(formula = y ~ x, data = df)
> 
>  
> 
> Coefficients:
> 
> (Intercept)            x  
> 
>   1.000e+00    9.463e-18  
> 
>  
> 
>> summary(myl)
> 
>  
> 
> Call:
> 
> lm(formula = y ~ x, data = df)
> 
>  
> 
> Residuals:
> 
>        Min         1Q     Median         3Q        Max 
> 
> -1.136e-16 -1.341e-17  7.886e-18  2.918e-17  5.047e-17 
> 
>  
> 
> Coefficients:
> 
>              Estimate Std. Error   t value Pr(>|t|)    
> 
> (Intercept) 1.000e+00  3.390e-17 2.950e+16   <2e-16 ***
> 
> x           9.463e-18  5.463e-18 1.732e+00    0.122    
> 
> ---
> 
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
> 
>  
> 
> Residual standard error: 4.962e-17 on 8 degrees of freedom
> 
> Multiple R-squared: 0.7145,     Adjusted R-squared: 0.6788 
> 
> F-statistic: 20.02 on 1 and 8 DF,  p-value: 0.002071
> 
>  
> 
> 
> 	[[alternative HTML version deleted]]
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Ehlers
University of Calgary
403.202.3921



More information about the R-help mailing list