title: How to include standard error input values in weighted linear regression?
body: <p>In first step I have made linear regression for every array of 10 points for 25 devices as in the plot</p>
  <p><a href="https://i.stack.imgur.com/6qnTR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6qnTR.png" alt="enter image description here" /></a></p>
  <p>I get resistance R = 1/slope for every device and SE(standard error). Every 5 out of 25 devices have different length, as in the table:</p>
  <p><a href="https://i.stack.imgur.com/uRopu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uRopu.png" alt="enter image description here" /></a></p>
  <p>1 device in every length have a bad results which I exclude thanks to weighted linear regression.
  In the second step I made weighted linear regression of R and length and thanks to weight around 0 I excluded bad results. Black points are outliers</p>
  <p><a href="https://i.stack.imgur.com/8Xbms.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8Xbms.png" alt="enter image description here" /></a></p>
  <p>Here the correct values line plot is better visible:</p>
  <p><a href="https://i.stack.imgur.com/KOPd4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KOPd4.png" alt="enter image description here" /></a></p>
  <p>but I didn't include standard error of every R value and I don't know how to calculate the second standard error. Someone know how can I combine it into my weighted linear regression?</p>
  <p>Here Is my code:</p>
  <pre><code>import math
  import matplotlib.pyplot as plt
  from scipy import stats
  import numpy as np
  import pandas as pd
  from operator import add
  import random
  from sklearn.linear_model import LinearRegression
  
  
  def creating_electric_current_data():
      l_max_val = [24, 23, 22, 21]
      l_num_elements = [20, 20, 20, 20]
      for n in [9, 7, 5, 3, 1]:
          list_of_measurements = []
          noise = []
          for max_val, num_elements in zip(l_max_val, l_num_elements):
              device_measurement = list(np.linspace(0, max_val + 4 * n, num_elements))
              noise = [x / 1000 for x in random.sample(range(-10, 10), num_elements)]
              device_add_measurement_noise = list(map(add, device_measurement, noise))
              list_of_measurements.append(device_add_measurement_noise)
          bad_measurement = [x / 20 for x in random.sample(range(0, 20),20)]
          list_of_measurements.append(bad_measurement)
          current.append(list_of_measurements)
  
  def creating_devices_names():
      global device
      devices_id = list(range(1, 6, 1))
      for device in devices_id:
          devices.append('device_' + str(device))
      print('')
  
  def calculate_IV_linear_regression_and_plot(value = 0.9,min_slope = 1, max_slope=50):
      r_value_verification = value
      global length, device, slope, intercept, r_value, p_value, std_err
      fig1, ax1 = plt.subplots()
      for length, i in zip(lengths, range(0, len(lengths))):
          l_current = np.array(current[i])
          for c, device in zip(l_current, devices):
              ax1.scatter(voltage, c, marker='.')
  
              ax1.set_xlabel(&quot;Voltage[V]&quot;, fontweight='bold')
              ax1.set_ylabel(&quot;Current[A]&quot;, fontweight='bold')
              slope, intercept, r_value, p_value, std_err = stats.linregress(voltage, c)
              # calculate uncertainty of resistance
              std_err_slope = std_err
              n = len(c)
              resistance = 1 / slope
              R = 'R = {}'.format(round(resistance, 4))
              # annotate to the last point on plot
  
              ax1.annotate(R, (voltage[-1], c[-1]),size=7)
              std_err_resistance = std_err_slope
              u_resistance = std_err_resistance/math.sqrt(n)
              # calculate weight
              if r_value_verification &lt;= r_value or min_slope&lt;=slope&lt;=max_slope:
                  weight_of_resistance_result = r_value + 0.0000000001
              else:
                  weight_of_resistance_result = 0.0000000001
              u_weighted_resistance = 0
              # create a dataframe table of all parameters
              dev_params[length, device] = [slope,
                                            std_err_slope,
                                            resistance,
                                            u_resistance,
                                            intercept,
                                            weight_of_resistance_result,
                                            u_weighted_resistance,
                                            r_value,
                                            p_value,
                                            std_err, ]
              ax1.plot(voltage, slope * voltage + intercept)
  
  def create_dataframe_from_dictionary1():
      global dev_resistance_params, dev_resistance_params
      dev_resistance_params = pd.DataFrame(dev_params).T
      dev_resistance_params.columns = ['slope',
                                       'uncertainty_of_slope',
                                       'resistance',
                                       'uncertainty_of_resistance',
                                       'intercept',
                                       'weight_of_resistance_result',
                                       'u_weighted_resistance',
                                       'r_value',
                                       'p_value',
                                       'std_err']
      dev_resistance_params.index.names = ['lengths', &quot;devices&quot;]
  
  def calculate_weighted_average_for_every_resistance_lengths():
      global length, df
      for length in lengths:
          df = dev_resistance_params.xs(key=length, level='lengths')
          weighted_avg_resistances = round(np.average(df['resistance'], weights=df['weight_of_resistance_result']), 20)
          dict_weighted_average_resistance[length] = [weighted_avg_resistances, length]
  
  def create_dataframe_from_dictionary2(dictionary):
      dev_weighted_average_resistance = pd.DataFrame(dictionary).T
      dev_weighted_average_resistance.columns = ['resistance_avg', 'lengths_values']
      dev_weighted_average_resistance.index.names = ['lengths']
      return dev_weighted_average_resistance
  
  def calculate_linear_regression_for_resistance_per_fet_weighted():
  
      global slope, intercept, r_value, p_value, std_err, Rc, u_Rc
      X = np.array(dev_resistance_params.reset_index()['lengths']).reshape(-1,1)
      y = np.array(dev_resistance_params['resistance'])
  
      w = np.array(dev_resistance_params['weight_of_resistance_result'])
  
      WLS = LinearRegression()
  
      WLS.fit(X, y, sample_weight=w)
  
      slope, intercept = WLS.coef_, WLS.intercept_
      R = intercept
      Rc = R / 2
      u_Rc = 0
  
  
  def linear_regression_plot_resistances_per_fet_weighted():
      fig2, ax2 = plt.subplots()
      x = dev_resistance_params.reset_index()['lengths']
      y = dev_resistance_params['resistance']
      w = np.array(dev_resistance_params['weight_of_resistance_result'])
      ax2.scatter(x,y,c=w)
      x = np.linspace(0, 180, 100)
      ax2.plot(x, slope * x + intercept)
      ax2.plot(x, slope * x + intercept)
      ax2.set_xlabel(&quot;Lengths[mm]&quot;, fontweight='bold')
      ax2.set_ylabel(&quot;Resistance[\u03A9]&quot;, fontweight='bold')
  
      fig1, ax2 = plt.subplots()
      x = np.linspace(0, 180, 100)
      ax2.plot(x, slope * x + intercept)
      ax2.plot(x, slope * x + intercept)
      ax2.set_xlabel(&quot;Lengths[mm]&quot;, fontweight='bold')
      ax2.set_ylabel(&quot;Resistance[\u03A9]&quot;, fontweight='bold')
  
  if __name__ == '__main__':
      # Create list of devices name
      devices = []
      creating_devices_names()
  
      # Data used for calculation: lengths, voltage and current
      lengths = [10,20,40,80,160]
      voltage = np.linspace(0,5,20)
  
      current = []
      creating_electric_current_data()
  
      # Calculate linear regression for current-voltage to get resistance
      dev_params = dict()
      calculate_IV_linear_regression_and_plot()
  
      # Results dataframe table for current-voltage
      create_dataframe_from_dictionary1()
  
      # Calculate weighted average for every resistance lengths
      dict_weighted_average_resistance = dict()
      calculate_weighted_average_for_every_resistance_lengths()
  
      # Calculate linear regression for resistance per every 25 device weighted
      calculate_linear_regression_for_resistance_per_fet_weighted()
      linear_regression_plot_resistances_per_fet_weighted()
  
      plt.show()
  </code></pre>