[R] Subset function of lm() does not seem to work

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sat Feb 7 12:00:46 CET 2004


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




More information about the R-help mailing list