EX NO:03                Simulate a regression model for a given dataset.
DATE:
AIM:
       The aim is to simulate a regression model for a given datasheet using the fitlm
       function.
SOFTWARE REQUIRED:
  •    MATLAB
  •    Open Source Tools
PROCEDURE:
          1. Click on the MATLAB Icon on the desktop.
          2. Click on the ‘FILE’ Menu on menu bar.
          3. Click on NEW M-File from the file Menu.
          4. Save the file in directory.
          5. Click on DEBUG from Menu bar and Click Run.
          6. Open the command window\ Figure window for the output
THEORY:
       Linear Regression using Least Squares in MATLAB
       Linear regression is a statistical method used to model the relationship between an
       independent variable (X) and a dependent variable (Y) by fitting a linear equation of the
       form:
                                           Y=mX+cY = mX + c
       where m is the slope (rate of change of Y with respect to X), and c is the intercept
       (value of Y when X = 0).
       In MATLAB, the fitlm function performs Ordinary Least Squares (OLS) Regression,
       which minimizes the sum of squared differences between the observed values (Y) and
       the predicted values (Y_pred). This ensures the best-fit line that reduces error.
    PROGRAM:
    clear;
    clc;
    close all;
    filename = 'water_data.xlsx'; % Make sure this file is in the current directory
    data = readtable(filename);
    disp('Preview of the dataset:');
    disp(head(data));
    X = table2array(data(:, 1:end-1)); % Features: Temp, Humidity, Occupants, Devices
    Y = table2array(data(:, end));
    n = size(X, 1);
    idx = randperm(n);
    n_train = round(0.8 * n);
    X_train = X(idx(1:n_train), :);
    Y_train = Y(idx(1:n_train));
    X_test = X(idx(n_train+1:end), :);
    Y_test = Y(idx(n_train+1:end));
    X_train_aug = [ones(size(X_train, 1), 1) X_train];
    X_test_aug = [ones(size(X_test, 1), 1) X_test];
    beta = (X_train_aug' * X_train_aug) \ (X_train_aug' * Y_train);
    Y_pred = X_test_aug * beta;
    mse = mean((Y_test - Y_pred).^2);
    r2 = 1 - sum((Y_test - Y_pred).^2) / sum((Y_test - mean(Y_test)).^2);
    fprintf('Mean Squared Error (MSE): %.4f\n', mse);
    fprintf('R-Squared (R²): %.4f\n', r2);
    f igure;
    scatter(Y_test, Y_pred, 40, 'filled');
    hold on;
    plot([min(Y_test) max(Y_test)], [min(Y_test) max(Y_test)], 'r--', 'LineWidth', 2);
    xlabel('Actual Water Consumption (liters/day)');
    ylabel('Predicted Water Consumption (liters/day)');
    title('Actual vs Predicted Water Consumption');
    grid on;
prelab Questions:
    1. What is synthetic data and why do we use it in linear regression?
    2. What is the purpose of adding noise to the synthetic data?
    3. How do we interpret the slope and intercept in a linear regression model?
    4. What is the significance of the R-squared value in a linear regression model?
    5. What are the assumptions of a linear regression model?
Post-lab Questions:
  1. What are the estimated slope and intercept values from the synthetic data?
  2. How does the presence of noise affect the estimated coefficients in the regression model?
  3. What is the R-squared value of the fitted model, and what does it tell us about the model's
      performance?
  4. What steps can be taken if the linear regression model does not fit the data well?
RESULT:
CORE COMPETENCY:
                            MARKS ALLOCATION:
         Details            Marks                    Marks Awarded
                            Allotted
                                           BOOPATHI V          DHARANESH P
                                           (73772213110)       (73772213116)
       Preparation            20
       Conducting             20
  Calculation / Graphs        15
         Results              10
Basic understanding (Core     15
  competency learned)
          Viva                10
         Record               10
          Total               100
                                       `
                                                           Signature of faculty
FLOWCHART: