{"id":398,"date":"2012-04-06T11:18:30","date_gmt":"2012-04-06T15:18:30","guid":{"rendered":"http:\/\/web.colby.edu\/mgimond\/?p=398"},"modified":"2012-04-06T11:26:27","modified_gmt":"2012-04-06T15:26:27","slug":"how-n-influences-the-correlation-coefficient","status":"publish","type":"post","link":"https:\/\/web.colby.edu\/mgimond\/2012\/04\/06\/how-n-influences-the-correlation-coefficient\/","title":{"rendered":"How N influences the correlation coefficient"},"content":{"rendered":"<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n\r\n# What's the probability of getting a high correlation\r\n# from a very small sample of uncorrelated variables\r\nx = rnorm(200,50,20)\r\ny = rnorm(200,50,20)\r\n\r\n# Check that x and y are not correlated\r\ncor(x,y)  # THis should return an r close to 0\r\n\r\n# Grab a very small sample and compute the\r\n# correlation coefficient. Repeat this i times\r\n# to generate a distribution of possible r values\r\n\r\nn = c(3,5,10,20) # sample size. Try n=3,5,10 and 20\r\npar(mfrow=c(2,2))\r\n\r\nfor (j in 1:length(n)){\r\n  corxy =vector()\r\n  for (i in 1:10000){\r\n    xs = sample(x,n[j],replace=F)\r\n    ys = sample(y,n[j],replace=F)\r\n    corxy[i] = cor(xs,ys)\r\n  }\r\n\r\n  # What's the probabily of getting r\u00b2 &gt; 0.7\r\n  cor.ecdf = ecdf(corxy^2)\r\n  cor.p = 1 - cor.ecdf(0.7)\r\n\r\n  # Plot the distribution of r\u00b2\r\n  hist(corxy^2,breaks=40,col=&quot;bisque2&quot;,xlab=&quot;r\u00b2&quot;,\r\n       main=paste(&quot;n =&quot;,n[j],&quot;\\n&quot;,&quot;probability =&quot;,sprintf(&quot;%4.3f&quot;,cor.p)))\r\n\r\n  # Plot r\u00b2 = 0.7 on the histogram\r\n  abline(v=0.7,col=&quot;red&quot;)\r\n}\r\n<\/pre>\n<p><a href=\"http:\/\/web.colby.edu\/mgimond\/files\/2012\/04\/Rplot01.jpeg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-402\" src=\"http:\/\/web.colby.edu\/mgimond\/files\/2012\/04\/Rplot01.jpeg\" alt=\"\" width=\"673\" height=\"582\" srcset=\"https:\/\/web.colby.edu\/mgimond\/files\/2012\/04\/Rplot01.jpeg 673w, https:\/\/web.colby.edu\/mgimond\/files\/2012\/04\/Rplot01-300x259.jpg 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1199,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[12807],"tags":[],"_links":{"self":[{"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts\/398"}],"collection":[{"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/users\/1199"}],"replies":[{"embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/comments?post=398"}],"version-history":[{"count":5,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":407,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts\/398\/revisions\/407"}],"wp:attachment":[{"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/media?parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/categories?post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/tags?post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}