用 R 中的随机数据生成特定的异常值 [英] Generate specific outliers with random data in R

查看:45
本文介绍了用 R 中的随机数据生成特定的异常值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成如下图所示的集群异常值,以更深入地调查这种情况.

I am trying to produce a cluster outliers as in the figure below to investigate that situation in more depths.

我尝试过但一无所获,因为该数字有一个因变量和自变量.我想要同样的情况但是有多个自变量.所以,它将是一个因变量和一个矩阵自变量.

I tried but with nothing, because the figure has one dependent and independent variable. I want the same situation but with more than one independent variables. So, it will be one dependent variable and a matrix of independent variables.

我尝试的 R-Code 如下

My attempt R-Code was as below

n=50
p=2
x <- matrix(rnorm(n*p),ncol = p)
y <- rnorm(n)
b=quantile(x,probs = 0.95)
id=which(x>b)
no=length(id)
x[id]=rnorm(no,5,0.5)
y[id]=rnorm(1)+10

更新

我尝试了下面的代码,但还是和图中不一样

I try the following code but still is not the same as shown in the figure

xa=rnorm(50)
xb=runif(50,min = 0,max=400)
x=rbind(xa,xb)
y=rnorm(100)
plot(x,y)

推荐答案

你可以用

set.seed(1)

xa = runif(20,0,20)
xb = runif(5,50,60)
x  = c(xa,xb)

y  = c(runif(20,25,120),runif(5,30,40))

plot(x,y,xlab="Independent variable",ylab="Response variable",xlim=c(0,60),ylim=c(25,120),pch=16)

这篇关于用 R 中的随机数据生成特定的异常值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆