This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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.

data = thingSpeakRead(12397,'NumDays',1,'Fields',[3 4],'outputFormat','table');

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
R-squared: 0.000508,  Adjusted R-Squared: -0.000202
F-statistic vs. constant model: 0.715, p-value = 0.398

The values show the estimated regression coefficients for the linear model along with other statistical parameters.

See Also

Functions