Example Problems -simple linear regression - University Questions


Example

Determine the regression equation by finding the regression slope coefficient and the intercept value using the following data.
x  55 60  65 70  80
y  52 54  56 58  62



beta0=30
beta1=0.4
so the regression equation is  y=30+0.4x

Easy Method (Using Summations)

The regression equation is

y^=θ0+θ1x\hat{y}=\theta_0+\theta_1x

where

θ1=nxyxynx2(x)2\theta_1=\frac{n\sum xy-\sum x\sum y}{n\sum x^2-(\sum x)^2}

and

θ0=yθ1xn\theta_0=\frac{\sum y-\theta_1\sum x}{n}

This method only requires the columns xx, yy, x2x^2, and xyxy.


Step 1: Prepare the Table

        xyxy
        55    52    3025    2860
        60    54    3600    3240
        65    56    4225    3640
        70    58    4900    4060
        80    62    6400    4960


Σ      330   282   22150   18760

Also,

  • n=5n=5
  • x=330\sum x=330
  • y=282\sum y=282
  • x2=22150\sum x^2=22150
  • xy=18760\sum xy=18760

Step 2: Calculate the Slope

θ1=5(18760)330(282)5(22150)3302\theta_1= \frac{5(18760)-330(282)} {5(22150)-330^2}

Substitute the values:

=9380093060110750108900= \frac{93800-93060} {110750-108900} =7401850= \frac{740}{1850} =0.4=0.4


Step 3: Calculate the Intercept

θ0=yθ1xn\theta_0= \frac{\sum y-\theta_1\sum x}{n}

Substitute the values:

=282(0.4)(330)5= \frac{282-(0.4)(330)}{5} =2821325= \frac{282-132}{5} =1505= \frac{150}{5} =30=30


Step 4: Regression Equation

y^=30+0.4x\boxed{\hat{y}=30+0.4x}

Example

The values of x and their corresponding values of y are shown in the table below. Find the least square regression lineEstimate the value of y when x=10
x=0  1  2  3  4
y=2  3  5  4  6

Create Calculation Table

xx    
yy   x2x^2  xyxy
0 00
13 13
25 410
34 912
46 1624

🔸 Compute Sums

x=10,y=20\sum x = 10,\quad \sum y = 20
x2=30,xy=49\sum x^2 = 30,\quad \sum xy = 49
n=5n = 5

🔹 Formula for β1 (Slope)

β1=nxy(x)(y)nx2(x)2\beta_1 = \frac{n\sum xy - (\sum x)(\sum y)} {n\sum x^2 - (\sum x)^2}

🔸 Substitute values

β1=5(49)(10)(20)5(30)(10)2\beta_1 = \frac{5(49) - (10)(20)} {5(30) - (10)^2} =245200150100=4550=0.9= \frac{245 - 200}{150 - 100} = \frac{45}{50} = 0.9

🔹  Formula forβ0(Intercept)

β0=yβ1xn\beta_0 = \frac{\sum y - \beta_1 \sum x}{n}

🔸 Substitute values

β0=200.9(10)5\beta_0 = \frac{20 - 0.9(10)}{5} =2095=115=2.2= \frac{20 - 9}{5} = \frac{11}{5} = 2.2

✅ Regression Line

y=0.9x+2.2\boxed{y = 0.9x + 2.2}

✅ Estimate y when x=10


y=0.9(10)+2.2=9+2.2=11.2y = 0.9(10) + 2.2 = 9 + 2.2 = 11.2

✅ Final Answer

  • Regression line:
y=0.9x+2.2y = 0.9x + 2.2
  • Estimated value at x=10x = 10:
11.2\boxed{11.2}

Example


Use the following data to construct a linear regression model for the auto insurance premium as a function of driving experience.
DrivingExp            |5 |2   |12|9  |15 |6  |25 |16
MonthlyPremium    |64|87|50|71|44 |56 |42 |60

Construct Calculation Table
xx
 yy
x2x^2
xyxy
5    64    25    320
287    4    174
1250    144    600
971    81    639
1544    225    660
656    36    336
2542    625    1050
1660    256    960

Compute Sums

x=90,y=474\sum x = 90,\quad \sum y = 474
x2=1396,xy=4739\sum x^2 = 1396,\quad \sum xy = 4739
n=8n = 8

 Compute Slopeβ1

β1=nxy(x)(y)nx2(x)2\beta_1 = \frac{n\sum xy - (\sum x)(\sum y)} {n\sum x^2 - (\sum x)^2}

Substitute:

β1=8(4739)(90)(474)8(1396)(90)2\beta_1 = \frac{8(4739) - (90)(474)} {8(1396) - (90)^2}\text{Denominator} = 11168 - 8100 = 3068

Final:

β1=474830681.55\beta_1 = \frac{-4748}{3068} \approx -1.55


Compute Intercept β0

β0=yβ1xn\beta_0 = \frac{\sum y - \beta_1 \sum x}{n}

Substitute:

β0=474(1.55×90)8\beta_0 = \frac{474 - (-1.55 \times 90)}{8} =474+139.58=613.5876.69= \frac{474 + 139.5}{8} = \frac{613.5}{8} \approx 76.69

✅ Regression Model

y=1.55x+76.69\boxed{y = -1.55x + 76.69}


Example

Predict the price of a 1000 square feet house using the regression model generated from the following data.
Square feet     Price(Lakhs)
500                     5
900                     10
1200                   13
1500                   18
2000                   25
2500                   32
2700                   35

Construct Table

xx
yy
x2x^2
xyxy
500    5    250000    2500
900    10    810000    9000
1200    13    1440000    15600
1500    18    2250000    27000
2000    25    4000000    50000
2500    32    6250000    80000
2700    35    7290000    94500

Compute Sums

x=11300,y=138\sum x = 11300,\quad \sum y = 138
x2=22290000,xy=278600\sum x^2 = 22290000,\quad \sum xy = 278600
n=7n = 7

Compute Slopeβ1

nxy(x)(y)nx2(x)2\beta_1 = \frac{n\sum xy - (\sum x)(\sum y)} {n\sum x^2 - (\sum x)^2}

Substitute:

β1=7(278600)(11300)(138)7(22290000)(11300)2\beta_1 = \frac{7(278600) - (11300)(138)} {7(22290000) - (11300)^2}

Final:

β1=390800283400000.01379\beta_1 = \frac{390800}{28340000} \approx 0.01379

Compute Intercept β0



β0=yβ1xn\beta_0 = \frac{\sum y - \beta_1 \sum x}{n}

Substitute:

β0=138(0.01379×11300)7\beta_0 = \frac{138 - (0.01379 \times 11300)}{7} =138155.837=17.8372.55= \frac{138 - 155.83}{7} = \frac{-17.83}{7} \approx -2.55

✅ Regression Model

y=0.01379x2.55\boxed{y = 0.01379x - 2.55}

Predict Price for 1000 sq.ft

y=0.01379(1000)2.55y = 0.01379(1000) - 2.55
=13.792.55=11.24= 13.79 - 2.55 = 11.24

✅ Final Answer

  • Regression line:
y=0.01379x2.55\boxed{y = 0.01379x - 2.55}
  • Predicted price for 1000 sq.ft:
11.24 Lakhs
\boxed{11.24 \text{ Lakhs}}

Comments

Popular posts from this blog

Machine Learning PCCST503 Semester5 KTU CS 2024 Scheme - Dr Binu V P

Introduction to Machine Learning (ML)

Distinguishing Machine Learning from Traditional Programming