Using arrays in LINEST for multi variable regressions

bevercam

New Member
Joined
Feb 17, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Is there a way to combine two columns into a single array such that it can be used inside the LINEST function?

for example, LINEST(known_ys, known_xs, const, stats) where the known_xs are two different columns of the same length from two different tabs, as opposed to be adjoining columns
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
We can use a custom function to combine multiple ranges into an array. First, copy and paste the following custom function into a regular module (Visual Basic Editor [Alt+F11] >> Insert >> Module)...

VBA Code:
Option Explicit

Function ArrayUnion(ParamArray Arg() As Variant) As Variant
' Code: Juan Pablo González
' Spec: Aladin Akyurek
' May 4, 2003

    Dim TempUnion() As Variant
    Dim i As Long, Itm As Variant, Ctr As Long
  
    For i = LBound(Arg) To UBound(Arg)
        Arg(i) = Arg(i)
        If IsArray(Arg(i)) Then
            For Each Itm In Arg(i)
                Ctr = Ctr + 1
                ReDim Preserve TempUnion(1 To Ctr) As Variant
                TempUnion(Ctr) = Itm
            Next Itm
        Else
            Ctr = Ctr + 1
            ReDim Preserve TempUnion(1 To Ctr) As Variant
            TempUnion(Ctr) = Arg(i)
        End If
    Next i
  
    ArrayUnion = TempUnion

End Function

Then, you can use the following worksheet formula, as an example...

Code:
=LINEST(ArrayUnion(Sheet1!A2:A5,Sheet2!A2:A5),ArrayUnion(Sheet1!B2:B5,Sheet2!B2:B5),,TRUE)

Change the sheet names and ranges, accordingly.

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,160
Messages
6,129,215
Members
449,493
Latest member
JablesFTW

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top