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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Faiz Rasool
Dear list members,

I want to  save residuals above or less than a certain value to an R
object. I have performed a multiple linear regression, and now I want
to find out which cases have a residual of above + 2.5 and – 2.5.

Below I provide the R  commands I have used.

Reg<-lm(a~b+c+d+e+f) # perform multiple regression with a as the
dependent variable.

Residuals<-residuals(reg) # store residuals to an R object.
stdresiduals<-rstandard(reg) #save the standardized residuals.
#now I want to type a  command that will save the residuals  of
certain range to an object.

I realize that by plotting  the data I can quickly  see the  residuals
outside a certain boundtry, however, I am totally blind, so visually
inspecting the plot is not possible for me.

Thank you for any help.

Regards,
Faiz.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

Jeff Newmiller
Residuals are stored as a numeric vector. The R software comes with a document "Introduction to R" that discusses basic math functions and logical operators that can create logical vectors:

abs( stdresiduals ) > 2.5

It also discusses indexing using logical vectors:

stdresiduals[ abs( stdresiduals ) > 2.5 ]

Note that in most cases it is worth going  the extra step of making your example reproducible [1][2][3] because many problems arise from issues in the data or in code that you don't think is broken.

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

[2] http://adv-r.had.co.nz/Reproducibility.html

[3] https://cran.r-project.org/web/packages/reprex/index.html (read the vignette)
--
Sent from my phone. Please excuse my brevity.

On February 22, 2018 8:09:49 PM PST, faiz rasool <[hidden email]> wrote:

>Dear list members,
>
>I want to  save residuals above or less than a certain value to an R
>object. I have performed a multiple linear regression, and now I want
>to find out which cases have a residual of above + 2.5 and – 2.5.
>
>Below I provide the R  commands I have used.
>
>Reg<-lm(a~b+c+d+e+f) # perform multiple regression with a as the
>dependent variable.
>
>Residuals<-residuals(reg) # store residuals to an R object.
>stdresiduals<-rstandard(reg) #save the standardized residuals.
>#now I want to type a  command that will save the residuals  of
>certain range to an object.
>
>I realize that by plotting  the data I can quickly  see the  residuals
>outside a certain boundtry, however, I am totally blind, so visually
>inspecting the plot is not possible for me.
>
>Thank you for any help.
>
>Regards,
>Faiz.
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

Peter Dalgaard-2
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

> On 23 Feb 2018, at 05:56 , Jeff Newmiller <[hidden email]> wrote:
>
> Residuals are stored as a numeric vector. The R software comes with a document "Introduction to R" that discusses basic math functions and logical operators that can create logical vectors:
>
> abs( stdresiduals ) > 2.5
>
> It also discusses indexing using logical vectors:
>
> stdresiduals[ abs( stdresiduals ) > 2.5 ]
>
> Note that in most cases it is worth going  the extra step of making your example reproducible [1][2][3] because many problems arise from issues in the data or in code that you don't think is broken.
>
> [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
>
> [2] http://adv-r.had.co.nz/Reproducibility.html
>
> [3] https://cran.r-project.org/web/packages/reprex/index.html (read the vignette)
> --
> Sent from my phone. Please excuse my brevity.
>
> On February 22, 2018 8:09:49 PM PST, faiz rasool <[hidden email]> wrote:
>> Dear list members,
>>
>> I want to  save residuals above or less than a certain value to an R
>> object. I have performed a multiple linear regression, and now I want
>> to find out which cases have a residual of above + 2.5 and – 2.5.
>>
>> Below I provide the R  commands I have used.
>>
>> Reg<-lm(a~b+c+d+e+f) # perform multiple regression with a as the
>> dependent variable.
>>
>> Residuals<-residuals(reg) # store residuals to an R object.
>> stdresiduals<-rstandard(reg) #save the standardized residuals.
>> #now I want to type a  command that will save the residuals  of
>> certain range to an object.
>>
>> I realize that by plotting  the data I can quickly  see the  residuals
>> outside a certain boundtry, however, I am totally blind, so visually
>> inspecting the plot is not possible for me.
>>
>> Thank you for any help.
>>
>> Regards,
>> Faiz.
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

Alberto Garre
In reply to this post by Faiz Rasool
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.