Call R libraries.
#library(randtoolbox)
#library(ggplot2)
#library(HH)
Generate a normally distributed dataset x ranged between 0 and 1.5
set.seed(0.12345)
n = 125
mean = 0.5
sd = 0.165
x = 1.5*rnorm(n, mean, sd)
plot(x, xlim=c(1, 125), ylim=c(0, 1.5), main="pseudo-randomly generated data x ranged between 0 and 1.5")
Show the normal distribution of the artificially generated dataset x
hist(x, density=20, breaks=14, prob=TRUE, xlim=c(0, 1.5), ylim=c(0, 2.0), main="normal curve over histogram of the randomly generated dataset x")
curve(dnorm(x, mean=mean*1.5, sd=sd*1.5), col="darkblue", lwd=2, add=TRUE)
Generate normally distributed noise datasets with different sigma values
y = x
noise_mean = 0
noise_sigma1 = 0.05
noise_sigma2 = 0.20
noise_sigma3 = 0.35
noise1v1 = rnorm(n, noise_mean, noise_sigma1)
noise2v1 = rnorm(n, noise_mean, noise_sigma1)
noise1v2 = rnorm(n, noise_mean, noise_sigma2)
noise2v2 = rnorm(n, noise_mean, noise_sigma2)
noise1v3 = rnorm(n, noise_mean, noise_sigma3)
noise2v3 = rnorm(n, noise_mean, noise_sigma3)
x1 = x + noise1v1
y1 = y + noise2v1
plot(x1, y1-x1, xlim=c(0, 1.5), ylim=c(-1.5, 1.5), main="scatter plot between x and y-x for sigma noise 0.05")
abline(lm(y1-x1~x1))
Linear best fit slope for sigma noise 0.05
regmodel1=lm(y1-x1~x1)
regmodel1$coefficients
## (Intercept) x1
## 0.02816976 -0.03660075
Correlation coefficient for sigma noise 0.05
cor(x1, y1-x1)
## [1] -0.1107132
x2 = x + noise1v2
y2 = y + noise2v2
plot(x2, y2-x2, xlim=c(0, 1.5), ylim=c(-1.5, 1.5), main="scatter plot between x and y-x for sigma noise 0.20")
abline(lm(y2-x2~x2))
Linear best fit slope for sigma noise 0.20
regmodel2=lm(y2-x2~x2)
regmodel2$coefficients
## (Intercept) x2
## 0.4501554 -0.6238248
Correlation coefficient for sigma noise 0.20
cor(x2, y2-x2)
## [1] -0.6461235
x3 = x + noise1v3
y3 = y + noise2v3
plot(x3, y3-x3, xlim=c(0, 1.5), ylim=c(-1.5, 1.5), main="scatter plot between x and y-x for sigma noise 0.35")
abline(lm(y3-x3~x3))
Linear best fit slope for sigma noise 0.35
regmodel3=lm(y3-x3~x3)
regmodel3$coefficients
## (Intercept) x3
## 0.5589441 -0.6977695
Correlation coefficient for sigma noise 0.35
cor(x3, y3-x3)
## [1] -0.5540875
The linear best fit slope for sigma noise 0.05 is -0.037.
The linear best fit slope for sigma noise 0.20 is -0.624.
The linear best fit slope for sigma noise 0.35 is -0.698.
The correlation coefficient for sigma noise 0.05 is -0.111.
The correlation coefficient for sigma noise 0.20 is -0.646.
The correlation coefficient for sigma noise 0.35 is -0.554.