Search code examples
pythontypeerrorlinear-regressionstatsmodels

TypeError float when fitting linear curve using Statsmodel but already converted the type


So I've been trying to write a general function to help me generate the regression results given historical data, and I didn't wanted to use the .predict function of statsmodels because it wouldn't give me the leeway of generating any best-fit linera line when given any factor and any dependent-variable to fit. But the thing is, when I try to use OLS, it keeps giving me this error message:

TypeError: 'float' object is not iterable

But this is confusing because I tried to convert the type of my dependent variable to listlist, and to nd.array but somehow it keeps changing into float type. And I don't know how to solve this.

For clarification, I am going to put my code here:

#############REGRESSION###################
import csv
from scipy import stats
import statsmodels.api as sm
from scipy.optimize import curve_fit
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf  
from statsmodels.tsa.stattools import adfuller
from sklearn.model_selection import train_test_split
from pmdarima.arima.utils import ndiffs  
# Import the library
import pmdarima as pm
from pmdarima import auto_arima
# Fit a SARIMAX(0, 1, 1)x(2, 1, 1, 12) on the training set
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.tsa.arima_model import ARIMA 
from statsmodels.tsa.seasonal import seasonal_decompose

#First, three stock factors with GDP
GDP=list(df1['GDP'])
GDP=GDP[:len(stockdf)]
GDP.reverse()
print(GDP)

def param(factor,stock):
    print(type(sm.add_constant(factor)))
    model = sm.OLS(list(stock), sm.add_constant(factor))
    
    #model=sm.OLS(factor,sm.add_constant(stock))
    p=model.fit().params.tolist()
    return p[1]*x+p[0]

#def findRegression(factor,stock):
    #return list(map(param,factor,stock))
#model=findRegression(GDP,stockdf['上证指数']
print(type(stockdf['SSE']))
stock=list(stockdf['SSE'])
print(type(stock))
model=list(map(param,GDP,stock))
#print(len(stockdf['上证指数']))
#print(stockdf['上证指数'])

also, the print(type()) at the end gives me:

<class 'pandas.core.series.Series'>
<class 'list'>
<class 'numpy.ndarray'>

but when I try to add type(stock) in my param(factor, stock) function, it tells me it is class 'float'.

Any help would be very much appreciated!


Solution

  • model=list(map(param,GDP,stock))
    

    When you use map on a function with a list, (I encourage you to read the map documentation), map is gonna "map" it for every element of your list

    Ence, when you are doing map(param,GDP,stock), you are not passing the stock list as a parameter to your function "param" but instead you are passing stock[0], stock[1]... stock[len(stock)-1]

    Example :

    def testing(val1, val2):
        print(val1)
        print(val2)
    
    
    
    a = ["1","2"]
    b = ["3","4"]
    
    model=list(map(testing,a,b))
    

    prints

    1 3
    2 4 
    

    Because the function testing gets called with each element from each array, ence your float type problem, when you do list(stock), you are doing list(2.5) (that's an example) which is not possible

    hope this is clear.