library(data.table)
dt <- data.table(Sector = c("Agriculture", "Manufacture", "Music", "Agriculture", "Manufacture", "Music",
"Agriculture", "Manufacture", "Music"),
Region = c("X", "X", "X", "Y", "Y", "Y", "Z", "Z", "Z"),
Year = c("2010", "2011", "2012", "2010", "2011", "2012", "2010", "2011", "2012"),
Number = c("238", "75", "1038", "150", "987", "156", "768", "398", "65"),
Population = c("200875", "200875", "200875", "375600", "375600", "375600", "492000",
"492000", "492000"))
dt[,`:=`(Year=as.numeric(Year),Number = as.numeric(Number),Population=as.numeric(Population))]
str(dt)
dt
dt[,cor(Number,Population),]
[1] -0.04352187
dt[,cor.test(Number,Population),]
Pearson's product-moment correlation
data: Number and Population
t = -0.11526, df = 7, p-value = 0.9115
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.6877646 0.6390714
sample estimates:
cor
-0.04352187
Correlation analysis plotting
dt[,plot(Number, Population),]
dt[,lattice::levelplot(cor(.SD[,4:5])),]
dt[,PerformanceAnalytics::chart.Correlation(.SD[,c(4,5)], histogram=TRUE, pch="+"),]
dt[,corrplot::corrplot(cor(.SD[,4:5]),is.corr = FALSE, win.asp = .7, method = "circle"),]
dt[,corrplot::corrplot.mixed(cor(.SD[,4:5]),lower.col = "black", number.cex = .7),]
dt[,corrplot::cor.mtest(.SD[,4:5])$p,]