Anna Bokovskaya - 1 year ago 208

R Question

When I use simple lm function. I have NA for some coefficients.

How can I create a new formula that removes all the coefficients that have NA in the summary?

**Example**:

`iris[which(iris$Species=="setosa"),]$Petal.Width<-1`

iris$SomeFactor<-round(log(rgeom(n = nrow(iris),prob = 0.1)+1))

model<-list()

for(i in unique(iris$Species)){

data<-iris[which(iris$Species==i),]

model[[i]]<-lm(formula = formula("Sepal.Length~Sepal.Width +

Petal.Length + as.factor(SomeFactor)+

Petal.Width"),data = data)

}

model[["setosa"]]

Call:

lm(formula = formula("Sepal.Length~Sepal.Width + Petal.Length + Petal.Width"),

data = data)

Coefficients:

(Intercept) Sepal.Width Petal.Length Petal.Width

2.3037 0.6674 0.2834 NA

And I want that for the group setosa the formula will be without Petal.Width.

just:

`Sepal.Length~Sepal.Width + Petal.Length + SomeFactor2+ SomeFactor3`

Usually, I use step function. and then take the formula from the model.

but it takes a lot of time. and reduce a lot of coefficients.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Use `drop.terms`

to, guess, drop the problem term. You will need to be carefull with the intercept term, it's in the formula by default. That's why I subtract 1 from `which`

below.

```
model<-list()
for(i in unique(iris$Species)){
data<-iris[which(iris$Species==i),]
model[[i]] <- lm(formula = formula("Sepal.Length~Sepal.Width + Petal.Length + Petal.Width"),data = data)
if(any(is.na(coef(model[[i]])))){
cf <- coef(model[[i]])
fmla <- drop.terms(terms(model[[i]]), which(is.na(cf)) - 1, keep.response = TRUE)
model[[i]] <- lm(fmla, data = data)
}
}
model[["setosa"]]
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**