<p>That basic regression will give you a rough guide, sometimes rougher than others. To get something accurate you'd have to model the topography, the dominant air currents, and the moderating effects of water, both in the form of the ocean and nearby rivers and lakes.</p>

CC BY-SA 4.0
2024-07-25T12:33:51.703Z
[Regression analysis](https://en.wikipedia.org/wiki/Regression_analysis) needs a model or fitting function, I don't see how one would *actually* do a "basic regression" - could you add some details? How would you compare that to [interpolation](https://earthscience.stackexchange.com/users/6031/uhoh) which seems a heck of a lot easier. Both are estimations, neither is correct, but one is a lot more straightforward to implement than the other.
uhoh
CC BY-SA 4.0
2024-07-25T12:38:21.270Z
@uhoh Regression *creates* a fitting function. Interpolation is also a function of regression models, you use regressions to interpolate data points that you don't have that fall within the space of known variables. Regression and interpolation are both gradient creation techniques used to create best fit lines/surfaces across the empty space between known data points. You may well be right that interpolation is both better and easier, not a programmer, but the OP has already suggested a model they feel comfortable using for the regression modelling.
Ash
CC BY-SA 4.0
2024-07-25T12:52:04.657Z
Thank you, I meant also interpolation, it's also an option. I also have the model of topography, and these stations also give wind intensity and wind directions (https://www.ipma.pt/pt/otempo/obs.superficie/).
João Pimentel Ferreira
CC BY-SA 4.0
2024-07-25T15:26:09.887Z
I think that for at least the *simplest* 2D interpolations (weighted averages of local points) one doesn't need gradients or iterations. However for irregularly spaced data you do have the challenge of finding the n closest points for each interpolation point, and have to take care with the weighting so that the result is at least not discontinuous. But I don't think my comments are particularly profound so I'll cut it out and go to sleep - it's past my bedtime here. :-)
uhoh
CC BY-SA 4.0
2024-07-27T01:49:39.773Z
Your caveats are all true but that is already step two whereas OP asked for step one. The question is about how to get an interpolation function to the given points in the first place, not how to choose a very good one among a lot of options that use a lot of extra data.
quarague
CC BY-SA 4.0
2024-07-25T12:11:44.250Z
2024-07-25T12:11:44.250Z
Ash
<p>Have you looked into <a href="https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm" rel="noreferrer">K Nearest Neighbors</a> regression? You can take the nearest k (say, 4) known points and average their temperatures. Has its drawbacks, but it's pretty good for interpolation. Readily implemented in <a href="https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsRegressor.html" rel="noreferrer">Scikit-learn</a> for Python.</p>

CC BY-SA 4.0
2024-07-25T20:45:04.300Z
2024-07-25T20:45:04.300Z
mikhail
<p>I suggest that you take a look at some of the spatial interpolation functions in <a href="https://qgis.org/" rel="nofollow noreferrer">QGIS</a>.  It is OpenSource GIS software that can handle both raster and vector data.   <a href="https://docs.qgis.org/3.34/en/docs/gentle_gis_introduction/spatial_analysis_interpolation.html" rel="nofollow noreferrer">Here's documentation</a> about using Inverse Distance Weighted (IDW) and Triangular Irregular Network (TIN), as well as mention of others available.  The example on that page is for unevenly distributed temperature data (just like you have).  And, <a href="https://www.qgistutorials.com/en/docs/3/interpolating_point_data.html" rel="nofollow noreferrer">here's a tutorial</a> on Interpolating Point Data.</p>

CC BY-SA 4.0
2024-07-25T22:20:46.177Z
2024-07-25T22:20:46.177Z
Adam
<p>The spatial variability of temperature anomalies (departures from a suitable average for a given time of year) is typically much smaller than the spatial variability of raw temperatures.  Consequently, a good first step is to first acquire or create a map of average (normal) temperatures, including topographic and coastal effects.  This is the &quot;first guess&quot;.  Then you can use any of several techniques to analyze the differences between the daily or instantaneous temperatures and the corresponding normal temperatures.  Add that analysis to the map of normal temperatures, and you have your complete analysis.</p>

CC BY-SA 4.0
2024-07-26T20:37:34.647Z
2024-07-26T20:38:02.370Z
2024-07-26T20:38:02.370Z
John N-G
<p>I think Kriging would be great for this. Its an interpolation method based on Gaussian processes that is used in geostatistics.</p>
<p>Here is a paper about it, that also takes noise and bias in your data into account:</p>
<p><a href="https://journals.plos.org/climate/article?id=10.1371/journal.pclm.0000216#sec005" rel="noreferrer">https://journals.plos.org/climate/article?id=10.1371/journal.pclm.0000216#sec005</a></p>

CC BY-SA 4.0
2024-07-26T21:27:37.857Z
interesting, there's even a JS library https://github.com/oeo4b/kriging.js/tree/master
João Pimentel Ferreira
CC BY-SA 4.0
2024-07-27T19:43:15.047Z
I already implemented kriging as you suggested, but I'm struggling a lot to find the correct sigma2 and alpha, as I get totally non-sense results. Do you have any idea on how to proceed on chosing these parameters?
João Pimentel Ferreira
CC BY-SA 4.0
2024-07-29T17:35:45.287Z
@JoãoPimentelFerreira Some implementations will automatically optimize covariance parameters, like fields (in R) and apparently pykrige (Python). If you want to do it explicitly, a common approach is to fit the variogram. You can also use maximum likelihood estimation or cross-validation, more rigorously.
damp_civil
CC BY-SA 4.0
2024-07-26T13:21:06.113Z
2024-07-26T13:21:06.113Z
Fruitdruif
<p>Beforehand, thanks a lot to everyone that replied, I got really good input. For the sake of transparency and open information, I share how did I finally implement it:</p>
<ul>
<li>for every X minutes (ex.: 15 minutes) the algorithm fetches the temperature data from the meteo stations via its API,</li>
<li>then it creates for the temperature a <a href="https://en.wikipedia.org/wiki/Triangulated_irregular_network" rel="nofollow noreferrer">Triangulated Irregular Network</a> (TIN), sorting each triangle of the TIN by area/surface,</li>
<li>Then, for every incoming request with the coordinates of the point to analyse, it finds which triangles of the TIN contain the point, and gets the first 5 of those triangles (i.e., finds the 5 smallest triangles of the TIN which contain the point),</li>
<li>Then for every of these 5 triangle it makes a simple linear interpolation to deduce the temperature on that requested point,</li>
<li>Then it computes the average of those 5 temperatures to get the final computed temperature.</li>
<li>If the point does not fit within any triangle of the Triangulated Irregular Network, simply finds the nearest point/station instead.</li>
</ul>
<p>I used the library <a href="https://turfjs.org/docs/api/tin" rel="nofollow noreferrer">TurfJS</a> since my project is developed in NodeJS</p>

CC BY-SA 4.0
2024-08-02T15:36:08.557Z
It's always encouraged to add your own answer post to your question if you've found a solution that works best for you. *Welcome to Earth Science!*
uhoh
CC BY-SA 4.0
2024-08-02T14:21:53.633Z
2024-08-02T14:21:53.633Z
João Pimentel Ferreira
<p>Imagine I have a map with known current temperatures measured at certain map points provided by meteorologic stations:</p>
<p><a href="https://i.sstatic.net/oTqyCm8A.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/oTqyCm8A.png" alt="enter image description here" /></a></p>
<p>Source <a href="https://www.ipma.pt/pt/otempo/obs.superficie/" rel="nofollow noreferrer">here</a></p>
<p>What's the most robust/accurate model to deduce the temperature to any given point in the map? A polynomial regression or interpolation <code>f:R^2-&gt;R</code> would be ok? If there is anything for Javascript/NodeJS or Python would be amazing. I'm developing an API.</p>

CC BY-SA 4.0
2024-07-26T06:44:15.547Z
NB: that would be interpolation, not extrapolation.
gerrit
CC BY-SA 4.0
2024-07-26T11:02:22.550Z
@gerrit amended to "deduce"
João Pimentel Ferreira
<temperature><models><climate-models>
How to estimate temperature based on known points in a map?
João Pimentel Ferreira
# How to estimate temperature based on known points in a map?

(Asked by: username_0 on 2024-07-25)

Imagine I have a map with known current temperatures measured at certain map points provided by meteorologic stations:

[![enter image description here](https://i.sstatic.net/oTqyCm8A.png)](https://i.sstatic.net/oTqyCm8A.png)

Source [here](https://www.ipma.pt/pt/otempo/obs.superficie/)

What's the most robust/accurate model to deduce the temperature to any given point in the map? A polynomial regression or interpolation `f:R^2->R` would be ok? If there is anything for Javascript/NodeJS or Python would be amazing. I'm developing an API.

**Tags:** temperature, models, climate-models

> **Comments:**
> **username_1**: NB: that would be interpolation, not extrapolation.  
> **username_0**: @username_1 amended to "deduce"  

---

## 6 answers

### Answer by username_2 on 2024-07-25. Score: 9

That basic regression will give you a rough guide, sometimes rougher than others. To get something accurate you'd have to model the topography, the dominant air currents, and the moderating effects of water, both in the form of the ocean and nearby rivers and lakes.

> **Comments:**
> **username_3**: [Regression analysis](https://en.wikipedia.org/wiki/Regression_analysis) needs a model or fitting function, I don't see how one would *actually* do a "basic regression" - could you add some details? How would you compare that to [interpolation](https://earthscience.stackexchange.com/users/6031/username_3) which seems a heck of a lot easier. Both are estimations, neither is correct, but one is a lot more straightforward to implement than the other.  
> **username_2**: @username_3 Regression *creates* a fitting function. Interpolation is also a function of regression models, you use regressions to interpolate data points that you don't have that fall within the space of known variables. Regression and interpolation are both gradient creation techniques used to create best fit lines/surfaces across the empty space between known data points. You may well be right that interpolation is both better and easier, not a programmer, but the OP has already suggested a model they feel comfortable using for the regression modelling.  
> **username_0**: Thank you, I meant also interpolation, it's also an option. I also have the model of topography, and these stations also give wind intensity and wind directions (https://www.ipma.pt/pt/otempo/obs.superficie/).  
> **username_3**: I think that for at least the *simplest* 2D interpolations (weighted averages of local points) one doesn't need gradients or iterations. However for irregularly spaced data you do have the challenge of finding the n closest points for each interpolation point, and have to take care with the weighting so that the result is at least not discontinuous. But I don't think my comments are particularly profound so I'll cut it out and go to sleep - it's past my bedtime here. :-)  
> **username_4**: Your caveats are all true but that is already step two whereas OP asked for step one. The question is about how to get an interpolation function to the given points in the first place, not how to choose a very good one among a lot of options that use a lot of extra data.  

---

### Answer by username_5 on 2024-07-25. Score: 7

Have you looked into [K Nearest Neighbors](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) regression? You can take the nearest k (say, 4) known points and average their temperatures. Has its drawbacks, but it's pretty good for interpolation. Readily implemented in [Scikit-learn](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsRegressor.html) for Python.


---

### Answer by username_8 on 2024-07-26. Score: 5

I think Kriging would be great for this. Its an interpolation method based on Gaussian processes that is used in geostatistics.

Here is a paper about it, that also takes noise and bias in your data into account:

<https://journals.plos.org/climate/article?id=10.1371/journal.pclm.0000216#sec005>

> **Comments:**
> **username_0**: interesting, there's even a JS library https://github.com/oeo4b/kriging.js/tree/master  
> **username_0**: I already implemented kriging as you suggested, but I'm struggling a lot to find the correct sigma2 and alpha, as I get totally non-sense results. Do you have any idea on how to proceed on chosing these parameters?  
> **username_9**: @username_0 Some implementations will automatically optimize covariance parameters, like fields (in R) and apparently pykrige (Python). If you want to do it explicitly, a common approach is to fit the variogram. You can also use maximum likelihood estimation or cross-validation, more rigorously.  

---

### Answer by username_6 on 2024-07-25. Score: 3

I suggest that you take a look at some of the spatial interpolation functions in [QGIS](https://qgis.org/). It is OpenSource GIS software that can handle both raster and vector data. [Here's documentation](https://docs.qgis.org/3.34/en/docs/gentle_gis_introduction/spatial_analysis_interpolation.html) about using Inverse Distance Weighted (IDW) and Triangular Irregular Network (TIN), as well as mention of others available. The example on that page is for unevenly distributed temperature data (just like you have). And, [here's a tutorial](https://www.qgistutorials.com/en/docs/3/interpolating_point_data.html) on Interpolating Point Data.


---

### Answer by username_7 on 2024-07-26. Score: 3

The spatial variability of temperature anomalies (departures from a suitable average for a given time of year) is typically much smaller than the spatial variability of raw temperatures. Consequently, a good first step is to first acquire or create a map of average (normal) temperatures, including topographic and coastal effects. This is the "first guess". Then you can use any of several techniques to analyze the differences between the daily or instantaneous temperatures and the corresponding normal temperatures. Add that analysis to the map of normal temperatures, and you have your complete analysis.


---

### Answer by username_0 on 2024-08-02. Score: 1

Beforehand, thanks a lot to everyone that replied, I got really good input. For the sake of transparency and open information, I share how did I finally implement it:

* for every X minutes (ex.: 15 minutes) the algorithm fetches the temperature data from the meteo stations via its API,
* then it creates for the temperature a [Triangulated Irregular Network](https://en.wikipedia.org/wiki/Triangulated_irregular_network) (TIN), sorting each triangle of the TIN by area/surface,
* Then, for every incoming request with the coordinates of the point to analyse, it finds which triangles of the TIN contain the point, and gets the first 5 of those triangles (i.e., finds the 5 smallest triangles of the TIN which contain the point),
* Then for every of these 5 triangle it makes a simple linear interpolation to deduce the temperature on that requested point,
* Then it computes the average of those 5 temperatures to get the final computed temperature.
* If the point does not fit within any triangle of the Triangulated Irregular Network, simply finds the nearest point/station instead.

I used the library [TurfJS](https://turfjs.org/docs/api/tin) since my project is developed in NodeJS

> **Comments:**
> **username_3**: It's always encouraged to add your own answer post to your question if you've found a solution that works best for you. *Welcome to Earth Science!*  

---

