{"id":196,"date":"2011-10-26T09:30:41","date_gmt":"2011-10-26T13:30:41","guid":{"rendered":"http:\/\/web.colby.edu\/mgimond\/?p=196"},"modified":"2011-10-26T14:07:18","modified_gmt":"2011-10-26T18:07:18","slug":"pearsons-r-computed-for-individual-segments-of-a-sample","status":"publish","type":"post","link":"https:\/\/web.colby.edu\/mgimond\/2011\/10\/26\/pearsons-r-computed-for-individual-segments-of-a-sample\/","title":{"rendered":"Pearson&#8217;s r computed for individual segments of a sample"},"content":{"rendered":"<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# The following code snippet was inspired from a post\r\n# in Stats.StackExchange.com (link to the exact post\r\n# was lost)\r\n\r\n# Define number of intervals\r\nnum.int = 8\r\n\r\n# Generate simulated data\r\nx = rnorm(1000,0,1)\r\ne = rnorm(length(x),0,.4)\r\ny = x + e\r\n# Plot all data point with regression line\r\nplot(y~x, cex=.5, col=&quot;#DDDDDD&quot;, pch=16)\r\ndat.lm = lm( y~x )\r\nabline(dat.lm,col=&quot;#CCCCCC&quot;)\r\n\r\n# Define additional paramaters \r\nusr   = par(&quot;usr&quot;) # Needed to set extent to all later in code\r\ncor.y = numeric()\r\nj     = 1\r\nint = diff(range(y))\/num.int # incremental interval\r\n\r\n# Loop through each segement and compute r and reg line\r\nfor (i in seq(min(y), max(y), int)){\r\n  xi = which(x &gt;= i &amp; x &lt; i+int)\r\n  cor.y[j] = cor(y[xi],x[xi])\r\n  clip(x1=i, x2=i+int, y1=min(y[xi]), y2=max(y[xi]))\r\n  sub.lm = lm( y[xi]~x[xi] )  \r\n  abline(sub.lm, col=&quot;blue&quot; )\r\n  do.call(&quot;clip&quot;, as.list(usr))  # reset to plot region\r\n  text(i+0.25,quantile(sub.lm$model[,1])[4],col=&quot;blue&quot;,\r\n       as.character(format(cor.y[j],digits=2)),cex=0.8)\r\n  j = j +1\r\n}\r\n\r\n# Plot overall r value\r\ndo.call(&quot;clip&quot;, as.list(usr))  # reset extent to plot region\r\ntext.r text(-2.5,3,text.r,col=&quot;#999999&quot;)\r\n<\/pre>\n<p><a href=\"http:\/\/web.colby.edu\/mgimond\/files\/2011\/10\/Rplot.jpeg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-243\" src=\"http:\/\/web.colby.edu\/mgimond\/files\/2011\/10\/Rplot.jpeg\" alt=\"\" width=\"479\" height=\"506\" srcset=\"https:\/\/web.colby.edu\/mgimond\/files\/2011\/10\/Rplot.jpeg 479w, https:\/\/web.colby.edu\/mgimond\/files\/2011\/10\/Rplot-283x300.jpg 283w\" sizes=\"(max-width: 479px) 100vw, 479px\" \/><\/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\/196"}],"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=196"}],"version-history":[{"count":24,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":248,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts\/196\/revisions\/248"}],"wp:attachment":[{"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}