Skip to content

Instantly share code, notes, and snippets.

@lukeasrodgers

lukeasrodgers/junk.r

Last active Feb 13, 2017
Embed
What would you like to do?
messing with R
# install CRAN package from R repl
install.packages("mypkg", lib="/Users/luke/.r/pkg/\/R-packages/")
#random numbers from normal distribution
set.seed(42) #for reproducible numbers
x = rnorm(5000) #generate random numbers from normal dist
hist(x,breaks=50, main="Normal distribution, N=5000") #plot
shapiro.test(x) #SW test
>W = 0.9997, p-value = 0.744
#random numbers from normal distribution, slight deviation
# concat two distributions, one N=100 with mean of 2
x = c(rnorm(4900),rnorm(100,2))
hist(x,breaks=50, main="Normal distribution N=4900 + normal distribution N=200, mean=2")
shapiro.test(x)
>W = 0.9965, p-value = 1.484e-09
# uniform distribution
#random numbers between -10 and 10
x = runif(5000, min=-10, max=10)
hist(x,breaks=50,main="evenly distributed numbers [-10;10], N=5000")
shapiro.test(x)
>W = 0.9541, p-value < 2.2e-16
#bimodal normal distributions, 4 sd apart
# concat two separate normal distributions
x = c(rnorm(2500, -2, 1),rnorm(2500, 2, 1))
hist(x,breaks=50,main="Normal distributions, 4 sd apart")
shapiro.test(x)
>W = 0.9464, p-value < 2.2e-16
# use Kolmogorov-Smirnov test against normal distribution for your data
ks.test(mydata, 'pnorm', mean=mean(rates), sd=sd(rates))
# plot kernel density function
plot(density(mydata))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment