Pergunta

I'm running GLMs in R but need to try some models without one level of the factor Year.

It's like:

Year<-as.factor(c(1996,1997,1998,1999,2000))
Shr<-as.numeric(c(1,32,1,50,42))
#... and other variables.

How do I 'exclude' only 1998 from my model? I need to take the whole row from all the others variables as well or the GLM won´t run because of difference on the number of rows.

Foi útil?

Solução

Even more easily, you can just use the subset argument of glm():

glm(...,data=all,subset=(Year != 1998))

Outras dicas

Assuming you have your data in a data.frame called da.fr, you can use

da.fr2<-da.fr[da.fr$Year!=1998,]

da.fr2$Year<-droplevels(da.fr2$Year)

The first line makes a new data.frame without any of the 1998 data. The second line will remove 1998 as a factor in Year, as it is no longer in the dataset.

Year<-as.factor(c(1996,1997,1998,1999,2000))
Shr<-as.numeric(c(1,32,1,50,42))

dat <- data.frame(Year=Year, Shr = Shr) # your data
#> dat
#  Year Shr
#1 1996   1
#2 1997  32
#3 1998   1
#4 1999  50
#5 2000  42

> levels(dat$Year)
#[1] "1996" "1997" "1998" "1999" "2000"

Depending on what you want to achieve you can:

dat2 <- dat[!(dat$Year %in% "1998") , ] # remove rows with 1998
#> dat2
#  Year Shr
#1 1996   1
#2 1997  32
#4 1999  50
#5 2000  42

levels(dat2$Year)
#[1] "1996" "1997" "1998" "1999" "2000" # the 1998 level remains

but you can also:

dat2$Year <- factor(dat2$Year) # also remove the level if you want
levels(dat2$Year)
#[1] "1996" "1997" "1999" "2000"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top