Folks, I have an elementary question about the lm() function, where I'm trying to exploit the "subset" feature, to make it use only a subset of the observations. My code is: ---------------------------------------------------------------------- A <- read.table(file="datafile.2", col.names=c("date","dlinrchf","dlusdchf","dljpychf","dldemchf")) # The file datafile.2 has 100 observations. # I want to do a regression using the first 25 only # Subset vector : the 1st 25 are on, the remaining 75 are off. window = c(rep(1,25), rep(0,75)) model <- lm(dlinrchf ~ dlusdchf + dljpychf + dldemchf, A, window) summary(model) ---------------------------------------------------------------------- This doesn't work: I get -- Coefficients: (3 not defined because of singularities) Estimate Std. Error t value Pr(>|t|) (Intercept) -3.088e-03 1.384e-19 -2.232e+16 <2e-16 *** dlusdchf NA NA NA NA dljpychf NA NA NA NA dldemchf NA NA NA NA I have tested using alternative software (stata) and the 1st 25 observations are quite fine for doing a regression. I find that even if I set window to rep(1,100) (i.e. a vector of 100 ones) the lm() does not work. I guess I'm making some mistake in using the "subset" capability of lm(). Any help will be most appreciated...
Ajay Shah <ajayshah at mayin.org> writes:> # Subset vector : the 1st 25 are on, the remaining 75 are off. > window = c(rep(1,25), rep(0,75)) > > model <- lm(dlinrchf ~ dlusdchf + dljpychf + dldemchf, A, window) > summary(model) > ---------------------------------------------------------------------- > > This doesn't work: I get --[singular model fit snipped] Subsetting works just like indexing, and:> window <- c(rep(1,25), rep(0,75)) > x <- rnorm(100) > x[window][1] 0.1900275 0.1900275 0.1900275 0.1900275 0.1900275 0.1900275 0.1900275 [8] 0.1900275 0.1900275 0.1900275 0.1900275 0.1900275 0.1900275 0.1900275 [15] 0.1900275 0.1900275 0.1900275 0.1900275 0.1900275 0.1900275 0.1900275 [22] 0.1900275 0.1900275 0.1900275 0.1900275 This is because> x[0]numeric(0)> x[1][1] 0.1900275 whereas> window <- c(rep(TRUE,25), rep(FALSE,75)) > x[window][1] 0.19002751 0.78343198 -0.52160803 -0.89006812 -1.00943372 -0.28583173 [7] 0.68715861 0.92054258 -0.61218864 -0.20847005 -0.34776935 0.49181302 [13] -0.70108931 -1.27045238 1.28170084 -0.43450470 -0.77251777 -0.73847935 [19] 1.69325725 1.40293178 -1.10930475 0.61299845 1.15125407 -0.02241302 [25] 0.22170428 and window <- 1:25 works as well. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907