# Remove effect of an independent variable on a dependent one

Currently, we are taking weight measures from a brick with a weight of 1kg as a way of calibration. However, the final weight returned by the sensor is not 1kg, instead, it varies along with ambient temperature, probably due to how the sensor estimates the weight (I attach the data scaled on a graph).

We want the returned measure to not be affected by the effect of temperature, i.e. we expect a horizontal line because the weight of the brick can not change. For this purpose, the temperature effect on the final weight should be removed. The idea is to take measurements from other sources such as plants, so that the effect of temperature on the weight marked by the scale could lead to erroneous estimates when working with the data in the future.

To do this, I have tried running a regression model, estimating the effect of temperature on weight over time. Once estimated, I held the temperature constant with a value such as the median or maximum temperature, so that the weight remained constant. Another idea was to subtract the estimated effect (from linear regression) of temperature on the actual weight measurements so that the weight also remains constant. However, I am not sure of the adequacy of these methods, any ideas on how I might achieve this goal?

Edit: As suggested in the comments I have added more information. First, the scatter plot of the two variables is shown below. As measurements were taken sequentially, I have represented the time variable as the total number of data points in order. Note that the variables are in their original scale. It seems that there is a negative linear relationship between weight and temperature.

Next, data points were collected each minute for a period of two days in a room with controlled temperature (between an interval of a few degrees). So the cyclic behavior observed should be the normal day/night cycle temperature plus the room trying to preserve the temperature between the predefined interval.

• You are on the right track with a regression approach. You can use the residuals from the regression of weight on temperature, since that amounts to a projection into the orthogonal complement of the space spanned by temperature. See the Frisch Waugh Lowell theorem for this partial regression framework Dec 15, 2023 at 14:02
• @YashaswiMohanty Thanks for your answer. After reading about the theorem, I am a bit confused about how I should apply it here. It seems that I need more than only one independent variable. For example, in the case where I want to know the relationship between two variables I also need to control for the effect of other variables. It is still applicable here? My apologies if I misunderstood something, I do have not a statistical background. Dec 18, 2023 at 9:26

First, a useful graph would be a scatter plot of temperature and weight. That would make it much clearer if the relationship is linear. It looks approximately linear from the graph you posted (with a negative correlation) but it's not easy to discern how close to linear it is.

Second, in addition the effect of temperature, there seems to be a cyclic time effect. You don't mark the scale of time in your graph, so it's impossible to tell what the cycle is, but something is going on.

Third, if you can hold the temperature constant (e.g. by always weighing at a temperature of 20 degrees C, or whatever) then I don't see why you would need the regression. The problem seems to be that you can't control the temperature in the real world, so, I would run a regression, maybe with a spline, and use that to adjust the weight.

• A GAM could work, but I was thinking of adding a spline of the variable. I wrote about them here towardsdatascience.com/restricted-cubic-splines-c0617b07b9a5 Dec 18, 2023 at 11:00

It appears you posted a plot of the data after Peter gave his useful answer, so I will add a post here based on the new information.

A GAM could work for this (it can model both the weight-temp relationship as well as time), but the biggest issue I foresee here is the problem of heterogeneity of variance. One can certainly fit this as-is and get good enough predictions, but the issues I see are the sparsity of data in some regions and overabundance of data in other areas, particularly in places like these:

There are two ways I would suggest tackling this. The first is to try to run a GAM but try to identify if these variance patterns are due to some other variables, particularly clustering (such as by-brick differences in effects). As an example, GAMs can predict by-brick random intercepts directly with splines, which would capture whatever is going on with these areas in your plot (if that is indeed what is causing this).

However, these trends may not be directly estimable by what is freely available in your dataset. What may be better in this case is to use something like a generalized additive model for location scale and shape (GAMLSS). These offer a lot more flexibility in that they can:

• Model pretty much any distribution, including custom distributions. Depending on your data, this may help model your regression more closely to what's going on under the hood.
• More importantly, they can allow the dependent variable to be predicted by different moments of the distribution. Particular to your problem, you could employ a location-scale model that estimates not just the mean of the dependent variable but also the variance as a function of your predictors.

I admit that these are very complicated and require a strong foundation in regression and GAM theory/programming before using them. I recommend reading Simon Wood's book first, followed by the first GAMLSS book listed here.

• I think the plot is symptomatic of some kind of omitted variable; possibly the same thing that is causing the cyclic behavior in the original plot. Dec 18, 2023 at 14:44