Richard M. Heiberger
2008-Jan-04 22:13 UTC
[R] predict.lm removes rownames for a single row. Why?
predict.lm keeps row names when working from several rows in newdata, but always removes rowname from a single row. The rownames are removed by the line in predict.lm predictor <- drop(X[, piv, drop = FALSE] %*% beta[piv]) What is the reason for that decision? I usually want to retain the row names. tmp <- data.frame(x=1:4, y=c(1,3,2,5)) tmp.lm <- lm(y ~ x, data=tmp) tmp.new <- data.frame(x=5:6, y=8:7) predict(tmp.lm, newdata=tmp.new, interval="confidence") ## rownames predict(tmp.lm, newdata=tmp.new[1,,drop=FALSE], interval="confidence")## no rownames
Prof Brian Ripley
2008-Jan-05 06:37 UTC
[R] predict.lm removes rownames for a single row. Why?
This is an inconsistency in drop:> x <- matrix(1:4, 4,1, dimnames=list(letters[1:4], NULL)) > x[,1] a 1 b 2 c 3 d 4> drop(x)a b c d 1 2 3 4> drop(x[1,,drop=FALSE])[1] 1 S does not do that, and I don't think R should, given its documentation. (Note that x[1,] also drops names in R and not S.) On Fri, 4 Jan 2008, Richard M. Heiberger wrote:> predict.lm keeps row names when working from several rows in > newdata, but always removes rowname from a single row. > The rownames are removed by the line in predict.lm > predictor <- drop(X[, piv, drop = FALSE] %*% beta[piv]) > What is the reason for that decision? I usually want to > retain the row names. > > tmp <- data.frame(x=1:4, y=c(1,3,2,5)) > tmp.lm <- lm(y ~ x, data=tmp) > tmp.new <- data.frame(x=5:6, y=8:7) > > predict(tmp.lm, newdata=tmp.new, interval="confidence") ## > rownames > predict(tmp.lm, newdata=tmp.new[1,,drop=FALSE], interval="confidence")## no > rownames > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595