{"id":11646,"date":"2022-01-27T15:44:39","date_gmt":"2022-01-27T23:44:39","guid":{"rendered":"https:\/\/threecloud.wpengine.com\/?p=11646"},"modified":"2023-09-19T15:20:24","modified_gmt":"2023-09-19T22:20:24","slug":"linear-regression-with-r-integrate-microsoft-power-bi","status":"publish","type":"post","link":"https:\/\/3cloudsolutions.com\/resources\/linear-regression-with-r-integrate-microsoft-power-bi\/","title":{"rendered":"Apply Linear Regressions with R and Integrate with Microsoft Power BI"},"content":{"rendered":"<p><img decoding=\"async\" style=\"width: 800px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/Images\/Blog%20\/BI%20and%20Reporting%20\/powerbi_microsoft.png\" alt=\"powerbi_microsoft\" width=\"800\" \/><\/p>\n<p>Microsoft was a late mover into the Data Discovery space when they first launched Power BI in late 2014, but since then they have closed the gap with many of the main competitors in that market. One of the ways they have been able to close that gap is with the integration of several open-source languages such as R. In this quick exercise we will create a simple linear regression model in R based on stock price data for Microsoft and then import that dataset into Power BI for visualization.<\/p>\n<h3>Getting the Data<\/h3>\n<p><a href=\"https:\/\/finance.yahoo.com\/\" rel=\" noopener\">Yahoo! Finance<\/a> is always a good source for stock data, and we can connect to a (CSV) file of the Microsoft data using the following link:<\/p>\n<p><strong>http:\/\/chart.finance.yahoo.com\/table.csv?s=MSFT&amp;a=6&amp;b=17&amp;c=2016&amp;d=7&amp;<\/strong><br \/>\n<strong>e=17&amp;f=2016&amp;g=d&amp;ignore=.csv<\/strong><\/p>\n<p>We will call this default dataset Microsoft_df. A preview of the dataset can be seen below.<\/p>\n<p><img decoding=\"async\" style=\"width: 412px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image1-1.png\" alt=\"ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image1-1\" width=\"412\" \/><\/p>\n<p>Once we have the dataset, we can run a few lines of R code to bring the data in and apply a simple linear regression against the Adj Close column, which is the closing stock price of Microsoft.<\/p>\n<p><strong>#Begin R Code<\/strong><\/p>\n<p><strong>install.packages(&#8216;data.table&#8217;)<\/strong><\/p>\n<p><strong>library(data.table)<\/strong><\/p>\n<p><strong>Microsoft_df &lt;- fread(&#8216;http:\/\/chart.finance.yahoo.com\/table.csv?<\/strong><\/p>\n<p><strong>s=MSFT&amp;a=6&amp;b=17&amp;c=2016&amp;d=7&amp;e=17&amp;f=2016&amp;g=d&amp;ignore=.csv&#8217;)<\/strong><\/p>\n<p><strong>head(Microsoft_df)<\/strong><\/p>\n<p><strong>Microsoft_df$RowCount&lt;-seq.int(nrow(Microsoft_df))<\/strong><\/p>\n<p><strong>StockRegression&lt;-lm(`Adj Close`~RowCount, Microsoft_df)<\/strong><\/p>\n<p><strong>New_DataFrame&lt;-cbind(Microsoft_df, Fitted=fitted(StockRegression))<\/strong><\/p>\n<p><strong>#End R Code<\/strong><\/p>\n<p>The new dataset after applying the linear regression model to the Adj Close can be seen below.\u00a0 We called it New_DataFrame.<\/p>\n<p><img decoding=\"async\" style=\"width: 514px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image2.png\" alt=\"ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image2\" width=\"514\" \/><\/p>\n<p>Two additional columns have been created: RowCount and Fitted column. The RowCount was used to assist with the regression calculation, and the Fittedcolumn was the output that we will want to visualize.<\/p>\n<h3>Connecting to Power BI<\/h3>\n<p>We are now ready to Get Data into Power BI.<\/p>\n<p><img decoding=\"async\" style=\"width: 524px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image3.png\" alt=\"ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image3\" width=\"524\" \/><\/p>\n<p>There are\u00a0 a plethora of data sources available for connectivity with Power BI. We just need to find the R Script connection.<\/p>\n<p>Of course, we want to make sure that R is installed on the same machine that has Power BI running!<\/p>\n<p><img decoding=\"async\" style=\"width: 527px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image4.png\" alt=\"ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image4\" width=\"527\" \/><\/p>\n<p>Once connected, we are prompted with a blank editor screen asking us for our R Script.<\/p>\n<p>We can copy and paste our R code into the editor and execute.<\/p>\n<p><img decoding=\"async\" style=\"width: 541px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image5.png\" alt=\"ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image5\" width=\"541\" \/><\/p>\n<p>When pasting into the Power BI editor, we do not need to include code for installing R packages. That is only required to be done once and through R itself.<\/p>\n<p><img decoding=\"async\" style=\"width: 538px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image6.png\" alt=\"ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image6\" width=\"538\" \/><\/p>\n<p>Once the script is executed, we view all of the available datasets that were produced from the R code we pasted in.<\/p>\n<p><img decoding=\"async\" style=\"width: 555px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image7.png\" alt=\"ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image7\" width=\"555\" \/><\/p>\n<p>In our case, we had our original dataset (Microsoft_df) and the derived dataset (New_DataFrame). For our purposes, we are only interested in loading theNew_DataFrame. Once the new dataset is loaded, we can select the appropriate fields for visualization and few the results.<\/p>\n<p><img decoding=\"async\" style=\"width: 493px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image8.png\" alt=\"ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image8\" width=\"493\" \/><\/p>\n<p>We can change the default view from a table to a line chart by selecting the visualization picker on the right-hand side.<\/p>\n<p><img decoding=\"async\" style=\"width: 175px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image9.png\" alt=\"ApplyLinearRegressionswithRandIntegrateMicrosoftPowerBi_image9\" width=\"175\" \/><\/p>\n<p>We can now see our new data visualized in Power BI with our original Adj Close as well as the Fitted points against the Date.<\/p>\n<p><img decoding=\"async\" style=\"width: 525px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/ANALYZE%20THE%20STOCK%20MARKET%20USING%20POWER%20BI_image16.png\" alt=\"ANALYZE THE STOCK MARKET USING POWER BI_image16\" width=\"525\" \/><\/p>\n<p>We can see that while Microsoft stock has had a few peaks and valleys over the 23-day period, overall, they seem to be trending upward according to this simple linear regression model.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Microsoft was a late mover into the Data Discovery space when they first launched Power&mldr;<\/p>\n","protected":false},"author":21,"featured_media":11858,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[394,260,297],"tags":[],"class_list":["post-11646","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-business-intelligence","category-data-ai","category-data-platform","topics-blog"],"acf":[],"_links":{"self":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/11646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/comments?post=11646"}],"version-history":[{"count":0,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/11646\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media\/11858"}],"wp:attachment":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media?parent=11646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/categories?post=11646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/tags?post=11646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}