{"id":15,"date":"2014-09-17T22:32:30","date_gmt":"2014-09-18T05:32:30","guid":{"rendered":"https:\/\/blogs.ubc.ca\/wtamagi\/?p=15"},"modified":"2014-09-18T13:34:18","modified_gmt":"2014-09-18T20:34:18","slug":"linear-regression-example-with-r","status":"publish","type":"post","link":"https:\/\/blogs.ubc.ca\/wtamagi\/2014\/09\/17\/linear-regression-example-with-r\/","title":{"rendered":"Linear Regression Example with R"},"content":{"rendered":"<p>Linear Regression using R<\/p>\n<p>&#8211; read.table(&#8220;c:\\\\users\\\\wayne\\\\downloads\\\\d.txt&#8221;,sep=&#8221; &#8220;,header=T)<br \/>\n&gt; x<br \/>\nconcentration absorb1 absorb2 absorb3<br \/>\n1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0 0.016\u00a0\u00a0 0.019\u00a0\u00a0 0.004<br \/>\n2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 10\u00a0\u00a0 0.101\u00a0\u00a0 0.128\u00a0\u00a0 0.118<br \/>\n3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 20\u00a0\u00a0 0.199\u00a0\u00a0 0.216\u00a0\u00a0 0.212<br \/>\n4\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 30\u00a0\u00a0 0.352\u00a0\u00a0 0.356\u00a0\u00a0 0.348<br \/>\n5\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 40\u00a0\u00a0 0.524\u00a0\u00a0 0.522\u00a0\u00a0 0.534<br \/>\n6\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 50\u00a0\u00a0 0.625\u00a0\u00a0 0.648\u00a0\u00a0 0.694<br \/>\n7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 60\u00a0\u00a0 0.701\u00a0\u00a0 0.712\u00a0\u00a0 0.705<br \/>\n&gt; absorb = c(x[[2]],x[[3]],x[[4]])<br \/>\n&gt; absorb<br \/>\n[1] 0.016 0.101 0.199 0.352 0.524 0.625 0.701 0.019 0.128 0.216 0.356 0.522<br \/>\n[13] 0.648 0.712 0.004 0.118 0.212 0.348 0.534 0.694 0.705<br \/>\n&gt; concent = c(rep(x[[1]],3))<br \/>\n&gt; concent<br \/>\n[1]\u00a0 0 10 20 30 40 50 60\u00a0 0 10 20 30 40 50 60\u00a0 0 10 20 30 40 50 60<br \/>\n&gt; xdata = data.frame(concent,absorb)<br \/>\n&gt; xdata<br \/>\nconcent absorb<br \/>\n1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0 0.016<br \/>\n2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 10\u00a0 0.101<br \/>\n3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 20\u00a0 0.199<br \/>\n4\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 30\u00a0 0.352<br \/>\n5\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 40\u00a0 0.524<br \/>\n6\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 50\u00a0 0.625<br \/>\n7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 60\u00a0 0.701<br \/>\n8\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0 0.019<br \/>\n9\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 10\u00a0 0.128<br \/>\n10\u00a0\u00a0\u00a0\u00a0\u00a0 20\u00a0 0.216<br \/>\n11\u00a0\u00a0\u00a0\u00a0\u00a0 30\u00a0 0.356<br \/>\n12\u00a0\u00a0\u00a0\u00a0\u00a0 40\u00a0 0.522<br \/>\n13\u00a0\u00a0\u00a0\u00a0\u00a0 50\u00a0 0.648<br \/>\n14\u00a0\u00a0\u00a0\u00a0\u00a0 60\u00a0 0.712<br \/>\n15\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0 0.004<br \/>\n16\u00a0\u00a0\u00a0\u00a0\u00a0 10\u00a0 0.118<br \/>\n17\u00a0\u00a0\u00a0\u00a0\u00a0 20\u00a0 0.212<br \/>\n18\u00a0\u00a0\u00a0\u00a0\u00a0 30\u00a0 0.348<br \/>\n19\u00a0\u00a0\u00a0\u00a0\u00a0 40\u00a0 0.534<br \/>\n20\u00a0\u00a0\u00a0\u00a0\u00a0 50\u00a0 0.694<br \/>\n21\u00a0\u00a0\u00a0\u00a0\u00a0 60\u00a0 0.705<\/p>\n<p>The format of your table will be two columns listing the concentration in one column and in the second column absorbance values.\u00a0 For other stats programs like Excel you will need to format your data in the spreadsheet like this.\u00a0 And then produce a scatterplot and a fitted line.<br \/>\n&gt; plot(concent,absorb)<\/p>\n<p>plots data, x-axis is concentration and y-axis is absorbance<\/p>\n<p>&gt; lm.data = lm(absorb~concent)<\/p>\n<p>Calculates linear regression model<\/p>\n<p>&gt; lines(concent,fitted(lm.data),col=&#8221;blue&#8221;)<\/p>\n<p>Adds fitted line to scatterplot.<\/p>\n<p>&gt; summary(lm.data)<\/p>\n<p>Summary of linear regression analysis<\/p>\n<p>Call:<br \/>\nlm(formula = absorb ~ concent)<\/p>\n<p>Residuals:<br \/>\nMin\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1Q\u00a0\u00a0\u00a0 Median\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3Q\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Max<br \/>\n-0.045119 -0.028119 -0.001952\u00a0 0.023214\u00a0 0.077381<\/p>\n<p>Coefficients:<br \/>\nEstimate Std. Error t value Pr(&gt;|t|)<br \/>\n(Intercept) -0.004214\u00a0\u00a0 0.012909\u00a0 -0.326\u00a0\u00a0\u00a0 0.748<br \/>\nconcent\u00a0\u00a0\u00a0\u00a0\u00a0 0.012417\u00a0\u00a0 0.000358\u00a0 34.681\u00a0\u00a0 &lt;2e-16 ***<\/p>\n<p>concent is the slope.\u00a0 Intercept is constant<\/p>\n<p>&#8212;<br \/>\nSignif. codes:\u00a0 0 \u2018***\u2019 0.001 \u2018**\u2019 0.01 \u2018*\u2019 0.05 \u2018.\u2019 0.1 \u2018 \u2019 1<\/p>\n<p>Residual standard error: 0.03281 on 19 degrees of freedom<br \/>\nMultiple R-squared:\u00a0 0.9844,\u00a0\u00a0\u00a0 Adjusted R-squared:\u00a0 0.9836<br \/>\nF-statistic:\u00a0 1203 on 1 and 19 DF,\u00a0 p-value: &lt; 2.2e-16<br \/>\n*** significance, probabilty no relationship exists beteween absorbance and concentration.<br \/>\nPr = probability this variable is no relevant<\/p>\n<p>absorbance = 0.0124*concentration + -0.00421<\/p>\n<p>Residuals = different between the actual values and the predicted values.<\/p>\n<p>R-squared indicates correlation between y and x (absorbance and concentration)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linear Regression using R &#8211; read.table(&#8220;c:\\\\users\\\\wayne\\\\downloads\\\\d.txt&#8221;,sep=&#8221; &#8220;,header=T) &gt; x concentration absorb1 absorb2 absorb3 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0 0.016\u00a0\u00a0 0.019\u00a0\u00a0 0.004 2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 10\u00a0\u00a0 0.101\u00a0\u00a0 0.128\u00a0\u00a0 0.118 3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 20\u00a0\u00a0 0.199\u00a0\u00a0 0.216\u00a0\u00a0 0.212 4\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 30\u00a0\u00a0 0.352\u00a0\u00a0 0.356\u00a0\u00a0 0.348 5\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 40\u00a0\u00a0 0.524\u00a0\u00a0 0.522\u00a0\u00a0 0.534 6\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 50\u00a0\u00a0 0.625\u00a0\u00a0 0.648\u00a0\u00a0 0.694 7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 60\u00a0\u00a0 0.701\u00a0\u00a0 0.712\u00a0\u00a0 0.705 &gt; absorb = c(x[[2]],x[[3]],x[[4]]) &gt; absorb [1] [&hellip;]<\/p>\n","protected":false},"author":25150,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/wtamagi\/wp-json\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/wtamagi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/wtamagi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/wtamagi\/wp-json\/wp\/v2\/users\/25150"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/wtamagi\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":3,"href":"https:\/\/blogs.ubc.ca\/wtamagi\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":19,"href":"https:\/\/blogs.ubc.ca\/wtamagi\/wp-json\/wp\/v2\/posts\/15\/revisions\/19"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/wtamagi\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/wtamagi\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/wtamagi\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}