📜  在 R 编程中向数值向量添加噪声 – jitter()函数

📅  最后修改于: 2022-05-13 01:55:24.096000             🧑  作者: Mango

在 R 编程中向数值向量添加噪声 – jitter()函数

在 R 编程中,抖动意味着向数值向量对象添加少量随机噪声。在本文中,我们将学习使用jitter()函数并创建一个绘图来可视化它们。

示例 1:

# Define numeric vectors
x <- round(runif(1000, 1, 10))
y <- x + rnorm(1000, mean = 0, sd = 5)
  
# output to be present as PNG file 
png(file="withoutJitter.png")
  
# Plotting without jitter function
plot(x, y, xlim = c(0, 11),
     main = "Without Jitter Function")
  
# saving the file 
dev.off()
  
x_j <- jitter(x)
  
# output to be present as PNG file 
png(file="withJitter.png")
  
# Plotting with jitter function
plot(x_j, y, xlim = c(0, 11),
     main = "With Jitter Function")
  
# saving the file 
dev.off()

输出:
无抖动

有抖动

示例 2:具有较大的因子值

# Define numeric vectors
x <- round(runif(1000, 1, 10))
y <- x + rnorm(1000, mean = 0, sd = 5)
  
# output to be present as PNG file 
png(file="withoutJitterFactor.png")
  
# Plotting without jitter function
plot(x, y, xlim = c(0, 11),
     main = "Without Jitter Function")
  
# saving the file 
dev.off()
  
x_j <- jitter(x, factor = 2)
  
# output to be present as PNG file 
png(file="withJitterFactor.png")
  
# Plotting with jitter function
plot(x_j, y, xlim = c(0, 11),
     main = "With Jitter Function and Large Factor")
  
# saving the file 
dev.off()

输出:
无抖动因子

带抖动因子