I tried toy problems and there doesn't seem to be a basic problem between lm() and ts objects: X = data.frame(x=c(1,2,7,9), y=c(7,2,3,1)) lm(y ~ x, X) X <- lapply(X, function(x) ts(x, frequency=12, start=c(1994,7))) lm(y ~ x, X) and this works fine - whether you do an lm() before or after making ts objects, it's okay. But I have a situation where things aren't okay. I have two happy time-series objects in a data frame:> M$g.cpi.iwJan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 1994 11.07 10.94 11.20 10.31 9.81 9.47 1995 9.89 9.81 9.74 9.67 10.29 10.47 11.39 10.92 10.07 10.38 10.31 9.69 ... (deleted) 2004 4.35 4.13 3.49 2.23 2.83 3.02 NA> M$g.wpiJan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 1994 11.14 11.83 11.88 12.66 13.20 14.54 1995 16.18 16.88 16.88 11.02 10.94 9.70 9.61 8.94 8.92 8.47 8.24 6.62 ... (deleted) 2004 6.45 6.14 4.79 4.52 4.99 6.14 6.86 But I can't get an OLS going:> lm(g.cpi.iw ~ g.wpi, M)Error in "storage.mode<-"(`*tmp*`, value = "double") : invalid time series parameters specified Any idea why? I think both objects are quite conformable (except for an NA, but that should get dropped by lm() by default). Here's a block of R code which creates M, in case you'd like to toy with this: M = structure(list(g.wpi = structure(c(11.14, 11.83, 11.88, 12.66, 13.2, 14.54, 16.18, 16.88, 16.88, 11.02, 10.94, 9.7, 9.61, 8.94, 8.92, 8.47, 8.24, 6.62, 5.04, 4.51, 4.53, 3.67, 3.58, 3.61, 4.28, 4.9, 5.08, 4.54, 4.48, 5.25, 5.12, 5.47, 5.43, 5.86, 5.09, 5.07, 3.6, 3.26, 3.83, 4.38, 3.94, 4.01, 5.11, 4.17, 4.32, 4.57, 5.64, 6.43, 7.1, 6.56, 5.89, 6.5, 7.15, 6.32, 4.48, 5.4, 5.41, 3.98, 3.31, 2.49, 1.97, 2.79, 3.23, 3.41, 3.1, 2.79, 3.58, 3.5, 5.54, 6.56, 6.35, 6.5, 6.51, 6.12, 6.47, 7.54, 7.61, 8.31, 8.69, 8.35, 6.45, 5.42, 5.6, 5.35, 5.27, 5.38, 4.48, 2.89, 2.59, 2.26, 1.53, 1.35, 1.72, 1.5, 1.5, 2.4, 2.79, 3.35, 3.55, 3.08, 3.39, 3.31, 4.21, 5.38, 6, 6.62, 6.57, 5.39, 4.68, 3.96, 4.9, 5.15, 5.42, 5.79, 6.45, 6.14, 4.79, 4.52, 4.99, 6.14, 6.86), .Tsp c(1994.5, 2004.5, 12), class = "ts"), g.cpi.iw = structure(c(11.07, 10.94, 11.2, 10.31, 9.81, 9.47, 9.89, 9.81, 9.74, 9.67, 10.29, 10.47, 11.39, 10.92, 10.07, 10.38, 10.31, 9.69, 9, 8.59, 8.87, 9.83, 9.33, 8.82, 8.31, 8.89, 8.52, 8.46, 8.72, 10.41, 11.11, 10.76, 10.03, 9.26, 7.32, 6.61, 5.6, 4.66, 4.94, 5.49, 4.87, 6.29, 9.71, 9.14, 8.26, 8.19, 10.51, 12.39, 14.8, 15.04, 16.34, 18.63, 19.67, 15.32, 9.38, 8.64, 8.95, 8.36, 7.71, 5.26, 3.16, 3.15, 2.14, 0.92, 0, 0.47, 2.62, 3.61, 4.83, 5.54, 5.01, 5.24, 4.95, 3.99, 3.5, 2.75, 2.74, 3.48, 3.25, 3.02, 2.53, 2.28, 2.5, 3.39, 4.04, 5.19, 4.73, 4.23, 4.89, 5.16, 4.94, 5.19, 5.17, 4.69, 4.66, 4.16, 3.89, 3.86, 4.3, 4.06, 3.6, 3.2, 3.43, 3.86, 4.06, 5.12, 4.66, 4.41, 4.16, 3.1, 2.89, 3.29, 3.07, 3.72, 4.35, 4.13, 3.49, 2.23, 2.83, 3.02, NA), .Tsp = c(1994.5, 2004.5, 12), class "ts")), .Names = c("g.wpi", "g.cpi.iw"), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121"), class "data.frame") -- Ajay Shah Consultant ajayshah at mayin.org Department of Economic Affairs http://www.mayin.org/ajayshah Ministry of Finance, New Delhi
Ajay Shah <ajayshah <at> mayin.org> writes: : : But I can't get an OLS going: : : > lm(g.cpi.iw ~ g.wpi, M) : Error in "storage.mode<-"(`*tmp*`, value = "double") : : invalid time series parameters specified : : : M = structure(list(g.wpi = structure(c(11.14, 11.83, 11.88, 12.66, : 13.2, 14.54, 16.18, 16.88, 16.88, 11.02, 10.94, 9.7, 9.61, 8.94, 8.92, : 8.47, 8.24, 6.62, 5.04, 4.51, 4.53, 3.67, 3.58, 3.61, 4.28, 4.9, 5.08, : 4.54, 4.48, 5.25, 5.12, 5.47, 5.43, 5.86, 5.09, 5.07, 3.6, 3.26, 3.83, : 4.38, 3.94, 4.01, 5.11, 4.17, 4.32, 4.57, 5.64, 6.43, 7.1, 6.56, 5.89, : 6.5, 7.15, 6.32, 4.48, 5.4, 5.41, 3.98, 3.31, 2.49, 1.97, 2.79, 3.23, : 3.41, 3.1, 2.79, 3.58, 3.5, 5.54, 6.56, 6.35, 6.5, 6.51, 6.12, 6.47, : 7.54, 7.61, 8.31, 8.69, 8.35, 6.45, 5.42, 5.6, 5.35, 5.27, 5.38, 4.48, : 2.89, 2.59, 2.26, 1.53, 1.35, 1.72, 1.5, 1.5, 2.4, 2.79, 3.35, 3.55, : 3.08, 3.39, 3.31, 4.21, 5.38, 6, 6.62, 6.57, 5.39, 4.68, 3.96, 4.9, : 5.15, 5.42, 5.79, 6.45, 6.14, 4.79, 4.52, 4.99, 6.14, 6.86), .Tsp : c(1994.5, 2004.5, 12), class = "ts"), g.cpi.iw = structure(c(11.07, : 10.94, 11.2, 10.31, 9.81, 9.47, 9.89, 9.81, 9.74, 9.67, 10.29, 10.47, : 11.39, 10.92, 10.07, 10.38, 10.31, 9.69, 9, 8.59, 8.87, 9.83, 9.33, : 8.82, 8.31, 8.89, 8.52, 8.46, 8.72, 10.41, 11.11, 10.76, 10.03, 9.26, : 7.32, 6.61, 5.6, 4.66, 4.94, 5.49, 4.87, 6.29, 9.71, 9.14, 8.26, 8.19, : 10.51, 12.39, 14.8, 15.04, 16.34, 18.63, 19.67, 15.32, 9.38, 8.64, : 8.95, 8.36, 7.71, 5.26, 3.16, 3.15, 2.14, 0.92, 0, 0.47, 2.62, 3.61, : 4.83, 5.54, 5.01, 5.24, 4.95, 3.99, 3.5, 2.75, 2.74, 3.48, 3.25, 3.02, : 2.53, 2.28, 2.5, 3.39, 4.04, 5.19, 4.73, 4.23, 4.89, 5.16, 4.94, 5.19, : 5.17, 4.69, 4.66, 4.16, 3.89, 3.86, 4.3, 4.06, 3.6, 3.2, 3.43, 3.86, : 4.06, 5.12, 4.66, 4.41, 4.16, 3.1, 2.89, 3.29, 3.07, 3.72, 4.35, 4.13, : 3.49, 2.23, 2.83, 3.02, NA), .Tsp = c(1994.5, 2004.5, 12), class : "ts")), .Names = c("g.wpi", "g.cpi.iw"), row.names = c("1", "2", "3", : "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", : "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", : "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", : "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", : "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", : "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", : "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", : "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", : "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", : "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", : "114", "115", "116", "117", "118", "119", "120", "121"), class : "data.frame") :>From the above we see there is an NA in the last observation.If we delete that observation then it runs to completion: R> lm(g.cpi.iw ~ g.wpi, M, subset = -121) Call: lm(formula = g.cpi.iw ~ g.wpi, data = M, subset = -121) Coefficients: (Intercept) g.wpi 3.7952 0.5083
On Sat, 21 Aug 2004, Ajay Shah wrote:> I tried toy problems and there doesn't seem to be a basic problem > between lm() and ts objects: > > X = data.frame(x=c(1,2,7,9), y=c(7,2,3,1)) > lm(y ~ x, X) > X <- lapply(X, function(x) ts(x, frequency=12, start=c(1994,7))) > lm(y ~ x, X) > > and this works fine - whether you do an lm() before or after making ts > objects, it's okay. > > But I have a situation where things aren't okay. I have two happy > time-series objects in a data frame: > > > M$g.cpi.iw > Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec > 1994 11.07 10.94 11.20 10.31 9.81 9.47 > 1995 9.89 9.81 9.74 9.67 10.29 10.47 11.39 10.92 10.07 10.38 10.31 9.69 > ... (deleted) > 2004 4.35 4.13 3.49 2.23 2.83 3.02 NA > > > M$g.wpi > Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec > 1994 11.14 11.83 11.88 12.66 13.20 14.54 > 1995 16.18 16.88 16.88 11.02 10.94 9.70 9.61 8.94 8.92 8.47 8.24 6.62 > ... (deleted) > 2004 6.45 6.14 4.79 4.52 4.99 6.14 6.86 > > But I can't get an OLS going: > > > lm(g.cpi.iw ~ g.wpi, M) > Error in "storage.mode<-"(`*tmp*`, value = "double") : > invalid time series parameters specified > > Any idea why? I think both objects are quite conformable (except for > an NA, but that should get dropped by lm() by default).That's the problem: the row with a NA gets dropped but the tsp atribute does not get adjusted. BTW, try traceback() when you get an error. lm(g.cpi.iw ~ g.wpi, data = na.omit(M)) will work, since an explicit call to na.omit does the right thing re attributes. The difference is in the internal code which says /* need to transfer _all but dim_ attributes, possibly lost by subsetting in na.action. */ for ( i = length(ans) ; i-- ; ) copyMostAttrib(VECTOR_ELT(data, i),VECTOR_ELT(ans, i)); That's wrong in this case. I think it was a leap to assume that you could fit linear models to time series via lm. ts objects are not mentioned on the help page for lm, are they? Another trap is to assume that diff() will be respected by lm. -- 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