Fit a Linear Regression Model

This example shows how to fit a linear regression model for data in your ThingSpeak™ channel and calculate the regression coefficients in the data.

Read Data from the Weather Station ThingSpeak Channel

ThingSpeak channel 12397 contains data from the MathWorks® weather station, located in Natick, Massachusetts. The data is collected once every minute. Field 3 and 4 contain humidity and temperature data, respectively. Read the data over the last day from channel 12397 using the thingSpeakRead function.

Calculate Linear Regression Model

Describe linear relationship between a response (humidity) and one or more predictive terms (temperature). For example, 'Humidity ~ 1 + TemperatureF' describes a two-variable linear model relating humidity with temperature along with an intercept.

mdl = fitlm(data, 'Humidity~TemperatureF')
mdl =

Linear regression model:
Humidity ~ 1 + TemperatureF

Estimated Coefficients:
Estimate       SE        tStat       pValue
________    ________    _______    ___________

(Intercept)       49.448      1.7916       27.6    2.1811e-134
TemperatureF    0.038851    0.045941    0.84567        0.39788

Number of observations: 1410, Error degrees of freedom: 1408
Root Mean Squared Error: 4.39