[R] How to programme R to randomly replace some X values with Outliers

Michael Bedward michael.bedward at gmail.com
Sun Oct 3 09:33:11 CEST 2010


N <- 100
Nrep <- 5

X <- runif(N, 0, 10)
Y <- 6 + 2*X + rnorm(N, 0, 1)
X[ sample(which(Y < 15), Nrep) ] <- runif(Nrep, 15, 20)

Hope this helps,
Michael


On 3 October 2010 16:12, Hock Ann Lim <lim_ha at yahoo.com> wrote:
> Dear experts,
> I am a beginner of R.
> I'm looking for experts to guide me how to do programming in R in order to
> randomly replace 5 observations in X explanatory variable with outliers drawn
> from U(15,20) in sample size n=100. The replacement subject to y < 15.
>
> The ultimate goal of my study is to compare the std of y with and without the
> presence of outliers based on average of 1000 simulation.
>
> Info :
> X~U(0,10)
> Y=6+2X+norm(0,1)
>
> Thank you.
>
> Hock Ann
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
>



More information about the R-help mailing list