當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R語言 jitter()用法及代碼示例


在 R 編程中,抖動意味著向數字向量對象添加少量隨機噪聲。在本文中,我們將學習使用 jitter() 函數並創建一個圖表來可視化它們。

用法: jitter(x, factor)

參數:
x:表示數值向量
factor:表示因子規範的數值

範例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()

輸出:
withoutJitter



withJitter

範例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()

輸出:
withoutJitterFactor

withJitterFactor




相關用法


注:本文由純淨天空篩選整理自utkarsh_kumar大神的英文原創作品 Adding Noise to a Numeric Vector in R Programming – jitter() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。