[R] How to Save the residuals of an LM object greater or less than a certin value to an R object?

Alberto Garre garre.alberto at gmail.com
Sun Feb 25 12:17:06 CET 2018


Hi Peter,

the "residuals()" function returns the residuals of a model fitted using
the "lm" function. For instances, using the example included in the help of
lm:


ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)

my_res <- residuals(lm.D9)
print(my_res)

The object returned by "residuals()" is a vector, so you can make any
operation with it. For instance:

my_res[my_res >= 0.1]

About your second question, I don't entirely understand what you want. The
"which()" function returns the indexes for which the condition is TRUE. In
this case, that the absolute value is greater than 2.5.

Alberto Garre


> Also,
>
> which( abs( stdresiduals ) > 2.5 )
>
> will tell you which of the standardized residuals are bigger than 2.5 in
absolute value. It returns a vector of indices, as in
>
> > set.seed(1234)
> > x <- rnorm(100)
> > which (abs(x) > 2.5)
> [1] 62
> > x[62]
> [1] 2.548991
>
>
> -pd

	[[alternative HTML version deleted]]



More information about the R-help mailing list