在 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()
輸出:
範例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()
輸出:
相關用法
- R語言 is.numeric()用法及代碼示例
- R語言 which.min()用法及代碼示例
- R語言 which.max()用法及代碼示例
- R語言 sign()用法及代碼示例
- R語言 as.vector()用法及代碼示例
- R語言 is.vector()用法及代碼示例
- R語言 append()用法及代碼示例
- R語言 between()用法及代碼示例
- R語言 cumprod()用法及代碼示例
- R語言 cumsum()用法及代碼示例
- R語言 atanh()用法及代碼示例
- R語言 asinh()用法及代碼示例
- R語言 acosh()用法及代碼示例
- R語言 data.matrix()用法及代碼示例
- R語言 as.character()用法及代碼示例
- R語言 axis()用法及代碼示例
注:本文由純淨天空篩選整理自utkarsh_kumar大神的英文原創作品 Adding Noise to a Numeric Vector in R Programming – jitter() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。