[Rd] Potential bug in fitted.nls

Dave Armstrong d@rm@t46 @end|ng |rom uwo@c@
Wed Jan 25 15:59:37 CET 2023


Dear Colleagues,

I recently answered [this question]() on StackOverflow that identified 
what seems to be unusual behaviour with `stats:::nls.fitted()`. In 
particular, a null model returns a single fitted value rather than a 
vector of the same fitted value of `length(y)`.  The documentation 
doesn’t make it seem like this is the intended behaviour, so I’m not 
sure if it’s a bug, a “Wishlist” item or something that is working 
as intended even though it seems unusual to me.  I looked through the 
bug reporting page on the R project website and it suggested contacting 
the R-devel list in cases where the behaviour is not obviously a bug to 
see whether others find the behaviour equally unusual and I should 
submit a Wishlist item through Bugzilla.

Below is a reprex that shows how the fitted values of a model with just 
a single parameter is length 1, but if I multiply that constant by a 
vector of ones, then the fitted values are of `length(y)`.  Is this 
something that should be reported?

``` r
dat <- 
data.frame(y=c(80,251,304,482,401,141,242,221,304,243,544,669,638),
                   ones = rep(1, 13))
mNull1 <- nls(y ~ a, data=dat, start=c(a=mean(dat$y)))
fitted(mNull1)
#> [1] 347.6923
#> attr(,"label")
#> [1] "Fitted values"

mNull2 <- nls(y ~ a*ones, data=dat, start=c(a=mean(dat$y)))
fitted(mNull2)
#>  [1] 347.6923 347.6923 347.6923 347.6923 347.6923 347.6923 347.6923 
347.6923
#>  [9] 347.6923 347.6923 347.6923 347.6923 347.6923
#> attr(,"label")
#> [1] "Fitted values"
```

Created on 2023-01-25 by the [reprex 
package](https://reprex.tidyverse.org) (v2.0.1)
	[[alternative HTML version deleted]]



More information about the R-devel mailing list