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]]
FWIW, nlsr::nlxb() gives same answers. JN On 2023-01-25 09:59, Dave Armstrong wrote:> 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]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Doesn't nls() expect that the lengths of vectors on both sides of the formula match (if both are supplied)? Perhaps it should check for that. -Bill On Thu, Jan 26, 2023 at 12:17 AM Dave Armstrong <darmst46 at uwo.ca> wrote:> 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]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]
Reasonably Related Threads
- Potential bug in fitted.nls
- Obtain gradient at multiple values for exponential decay model
- Obtain gradient at multiple values for exponential decay model
- Getting an error calling MASS::boxcox in a function
- Getting an error calling MASS::boxcox in a function