Closed shabbychef closed 4 years ago
I would propose to stats
maintainers to replace var(c(f))
with min(var(c(f)))
?
Thanks for the report. The problem is that summary.lm()
is using c()
for its side-effect of dropping attributes (including dim
).
c.xts()
behaves consistently with c.zoo()
, which means it just calls rbind()
. Changing the behavior of c.xts()
would be inconsistent with zoo, and would likely break user code.
The best solution I can think of is to change var(c(f))
to var(as.vector(f))
. I'll submit a request on R's bugzilla tracker.
I did some digging before posting. I think I'll have a hard time convincing R-core to change summary.lm()
. That's a method to summary()
, and you're calling it on a "mlm"
object. Calling summary(mod)
works, because it dispatches to stats:::summary.mlm()
.
I'm going to close this because it's not a bug in xts, and I don't think it's going to affect any change in base R. Please let me know if I've missed something. I'm happy to continue the discussion. We can re-open, if needed.
Sys.setenv('_R_CHECK_LENGTH_1_CONDITION_'='true')
Sys.setenv('_R_CHECK_LENGTH_1_LOGIC2_'='true')
library(xts)
set.seed(1234)
n <- 20
X <- xts(rnorm(n),order.by=Sys.Date() + seq_len(n))
XX <- cbind(X,X^2)
mod <- lm(XX ~ 1)
summary(mod)
# Response X :
#
# Call:
# lm(formula = X ~ 1)
#
# Residuals:
# X
# Min -2.0950
# 1Q -0.5997
# Median -0.2782
# 3Q 0.5660
# Max 2.6665
#
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) -0.2507 0.2267 -1.106 0.283
#
# Residual standard error: 1.014 on 19 degrees of freedom
#
#
# Response X.2 :
#
# Call:
# lm(formula = X.2 ~ 1)
#
# Residuals:
# X.2
# Min -1.03509
# 1Q -0.79025
# Median -0.57280
# 3Q -0.09958
# Max 4.79701
#
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 1.0392 0.3656 2.843 0.0104 *
# ---
# Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#
# Residual standard error: 1.635 on 19 degrees of freedom
#
Thanks for digging into this. The big reveal is that I wasn't calling summary.lm
, but rather a method of the sandwich
package (I think it was bread
) was explicitly calling summary.lm
for me. I didn't want to bring sandwich
into this if I could avoid doing so.
For an
xts
object,c(object)
behaves differentlyI found a future bug related to checking for non-scalar logicals. This may be an error in
xts
or instats
, or some mismatch in between. The following throws an error:I have tracked down the problem in
summary.lm
as follows:The fitted values will be an
xts
object, and the call toc(f)
results in thatxts
object back again; computing the variance then gives a 2x2 matrix instead of a scalar and the scalar logical check can throw an error. I should note that you do not get an error if you cast thexts
tomatrix
first, this runs fine:In this case
c(f)
returns an array, the variance of which is a scalar.Expected behavior
Not throw an error in
summary.lm
. Perhaps behave like a matrix with respect toc()
, or provide a convincing case tostats
package maintainers to fix their code.Minimal, reproducible example
Session Info