{"id":53,"date":"2026-03-03T09:47:45","date_gmt":"2026-03-03T09:47:45","guid":{"rendered":"https:\/\/gigz.pk\/r\/?post_type=lesson&#038;p=53"},"modified":"2026-04-01T12:09:01","modified_gmt":"2026-04-01T12:09:01","slug":"linear-regression-model","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/r\/lesson\/linear-regression-model\/","title":{"rendered":"\u00a0Linear Regression Model"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Linear regression is one of the most widely used statistical methods for predicting a continuous dependent variable based on one or more independent variables. R provides a simple and efficient way to build, evaluate, and visualize linear regression models.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>1. What is Linear Regression?<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Linear regression models the relationship between a dependent variable <math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><mi>Y<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">Y<\/annotation><\/semantics><\/math>Y and one or more independent variables <math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><mi>X<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">X<\/annotation><\/semantics><\/math>X by fitting a linear equation:<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>Y<\/mi><mo>=<\/mo><msub><mi>\u03b2<\/mi><mn>0<\/mn><\/msub><mo>+<\/mo><msub><mi>\u03b2<\/mi><mn>1<\/mn><\/msub><msub><mi>X<\/mi><mn>1<\/mn><\/msub><mo>+<\/mo><msub><mi>\u03b2<\/mi><mn>2<\/mn><\/msub><msub><mi>X<\/mi><mn>2<\/mn><\/msub><mo>+<\/mo><mo>\u22ef<\/mo><mo>+<\/mo><mi>\u03f5<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">Y = \\beta_0 + \\beta_1 X_1 + \\beta_2 X_2 + \\dots + \\epsilon<\/annotation><\/semantics><\/math>Y=\u03b20\u200b+\u03b21\u200bX1\u200b+\u03b22\u200bX2\u200b+\u22ef+\u03f5<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><msub><mi>\u03b2<\/mi><mn>0<\/mn><\/msub><\/mrow><annotation encoding=\"application\/x-tex\">\\beta_0<\/annotation><\/semantics><\/math>\u03b20\u200b = intercept<\/li>\n\n\n\n<li><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><msub><mi>\u03b2<\/mi><mn>1<\/mn><\/msub><mo separator=\"true\">,<\/mo><msub><mi>\u03b2<\/mi><mn>2<\/mn><\/msub><mo separator=\"true\">,<\/mo><mo>\u2026<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">\\beta_1, \\beta_2, \u2026<\/annotation><\/semantics><\/math>\u03b21\u200b,\u03b22\u200b,\u2026 = coefficients for predictors<\/li>\n\n\n\n<li><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><mi>\u03f5<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">\\epsilon<\/annotation><\/semantics><\/math>\u03f5 = error term<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Types of Linear Regression:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Simple Linear Regression:<\/strong> One predictor variable<\/li>\n\n\n\n<li><strong>Multiple Linear Regression:<\/strong> Two or more predictor variables<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>2. Load and Inspect Data<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\"># Load dataset<br>data &lt;- read.csv(\"customer_purchases.csv\")# Inspect data<br>head(data)<br>str(data)<br>summary(data)<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>3. Simple Linear Regression<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Predicting <code>PurchaseAmt<\/code> based on <code>Age<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Fit model<br>model_simple &lt;- lm(PurchaseAmt ~ Age, data = data)# Model summary<br>summary(model_simple)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key Outputs:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Coefficients:<\/strong> Effect of <code>Age<\/code> on <code>PurchaseAmt<\/code><\/li>\n\n\n\n<li><strong>R-squared:<\/strong> Proportion of variance explained by the model<\/li>\n\n\n\n<li><strong>p-value:<\/strong> Significance of predictors<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>4. Multiple Linear Regression<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Predicting <code>PurchaseAmt<\/code> based on <code>Age<\/code>, <code>Gender<\/code>, and <code>ProductCategory<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Fit model<br>model_multiple &lt;- lm(PurchaseAmt ~ Age + Gender + ProductCategory, data = data)# Summary<br>summary(model_multiple)<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>5. Making Predictions<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\"># Predict purchase amounts<br>predictions &lt;- predict(model_multiple, newdata = data)# View predicted vs actual<br>head(data.frame(Actual = data$PurchaseAmt, Predicted = predictions))<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>6. Evaluating Model Performance<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Common metrics for regression models:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Root Mean Squared Error (RMSE)<br>rmse &lt;- sqrt(mean((predictions - data$PurchaseAmt)^2))<br>rmse# Mean Absolute Error (MAE)<br>mae &lt;- mean(abs(predictions - data$PurchaseAmt))<br>mae<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>7. Visualizing Regression Results<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>a) Simple Linear Regression Plot<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">plot(data$Age, data$PurchaseAmt, main=\"Age vs Purchase Amount\", xlab=\"Age\", ylab=\"Purchase Amount\", pch=19, col=\"blue\")<br>abline(model_simple, col=\"red\", lwd=2)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>b) Multiple Regression with ggplot2<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">library(ggplot2)<br>ggplot(data, aes(x=Age, y=PurchaseAmt, color=Gender)) +<br>  geom_point(size=3) +<br>  geom_smooth(method=\"lm\", se=FALSE) +<br>  ggtitle(\"Multiple Linear Regression: Age &amp; Gender vs Purchase Amount\")<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>8. Assumptions of Linear Regression<\/strong><\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Linearity: Relationship between predictors and outcome is linear<\/li>\n\n\n\n<li>Independence: Observations are independent<\/li>\n\n\n\n<li>Homoscedasticity: Constant variance of residuals<\/li>\n\n\n\n<li>Normality: Residuals are normally distributed<\/li>\n\n\n\n<li>No multicollinearity: Predictors are not highly correlated<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Check assumptions using diagnostic plots:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">par(mfrow = c(2, 2))<br>plot(model_multiple)<br>par(mfrow = c(1, 1))<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>9. Advantages of Linear Regression<\/strong><\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simple and interpretable<\/li>\n\n\n\n<li>Provides insight into relationships between variables<\/li>\n\n\n\n<li>Basis for more advanced predictive models<\/li>\n\n\n\n<li>Easy to implement and visualize in R<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Linear regression in R is a fundamental technique for modeling and predicting continuous outcomes. By fitting simple or multiple regression models, making predictions, evaluating performance, and checking assumptions, you can extract actionable insights and understand relationships in your data. Mastering linear regression is a crucial step in data analysis and machine learning workflows.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/r\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">R Programming (R Lang) > R for Data Science &#038; Machine Learning > Linear Regression Model<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1775045317504\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":30,"template":"","class_list":["post-53","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>\u00a0Linear Regression Model - Analyze Deep. Visualize Better. Build with R.<\/title>\n<meta name=\"description\" content=\"Learn how to build and evaluate linear regression models in R. Master simple and multiple regression, predictions, and assumption checks.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0Linear Regression Model - Analyze Deep. Visualize Better. Build with R.\" \/>\n<meta property=\"og:description\" content=\"Learn how to build and evaluate linear regression models in R. Master simple and multiple regression, predictions, and assumption checks.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Analyze Deep. Visualize Better. Build with R.\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-01T12:09:01+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/lesson\\\/linear-regression-model\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"\u00a0Linear Regression Model - Analyze Deep. Visualize Better. Build with R.\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/#website\"},\"datePublished\":\"2026-03-03T09:47:45+00:00\",\"dateModified\":\"2026-04-01T12:09:01+00:00\",\"description\":\"Learn how to build and evaluate linear regression models in R. Master simple and multiple regression, predictions, and assumption checks.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/r\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"R Programming (R Lang) > R for Data Science & Machine Learning > Linear Regression Model\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/r\\\/\",\"name\":\"Analyze Deep. Visualize Better. Build with R.\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/r\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u00a0Linear Regression Model - Analyze Deep. Visualize Better. Build with R.","description":"Learn how to build and evaluate linear regression models in R. Master simple and multiple regression, predictions, and assumption checks.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0Linear Regression Model - Analyze Deep. Visualize Better. Build with R.","og_description":"Learn how to build and evaluate linear regression models in R. Master simple and multiple regression, predictions, and assumption checks.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Analyze Deep. Visualize Better. Build with R.","article_modified_time":"2026-04-01T12:09:01+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/r\/lesson\/linear-regression-model\/","url":"https:\/\/gigz.pk\/","name":"\u00a0Linear Regression Model - Analyze Deep. Visualize Better. Build with R.","isPartOf":{"@id":"https:\/\/gigz.pk\/r\/#website"},"datePublished":"2026-03-03T09:47:45+00:00","dateModified":"2026-04-01T12:09:01+00:00","description":"Learn how to build and evaluate linear regression models in R. Master simple and multiple regression, predictions, and assumption checks.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/r\/"},{"@type":"ListItem","position":2,"name":"R Programming (R Lang) > R for Data Science & Machine Learning > Linear Regression Model"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/r\/#website","url":"https:\/\/gigz.pk\/r\/","name":"Analyze Deep. Visualize Better. Build with R.","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/r\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/lesson\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/media?parent=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}