model-fitting

multivariable non-linear curve_fit with scipy

社会主义新天地 提交于 2019-12-11 04:24:32
问题 I have been trying to use scipy.optimize curve_fit using multiple variables. It works fine with the test code I created but when I try to implement this on my actual data I keep getting the following error TypeError: only arrays length -1 can be converted to python scalars The shape of the arrays and the data types of their elements in my test code and actual code are exactly the same so I am confused as to why I get this error. Test code: import numpy as np import scipy from scipy.optimize

How to get measures of model fit (AIC, F-statistics) in zelig for multiply imputed data?

三世轮回 提交于 2019-12-11 03:45:08
问题 Following up on an earlier post, I am interested in learning how to get the usual measures of the relative quality of a statistical model in zelig for regression using multiply imputed data (created with Amelia). require(Zelig) require(Amelia) data(freetrade) #Imputation of missing data a.out <- amelia(freetrade, m=5, ts="year", cs="country") # Regression model z.out <- zelig(polity~tariff+gdp.pc, model="ls", data=a.out$imputations) summary(z.out) Model: ls Number of multiply imputed data

Plotting estimates using ggplot2 & facet_wrap WITHOUT re-fitting models

拥有回忆 提交于 2019-12-11 00:33:45
问题 I am pretty new to ggplot2 and am looking to produce a figure with multiple scatter plots with their respective regression estimates. However I am using non-standard regression approaches (e.g quantile regression and total regression) that are not among the list of method arguments available in geom_smooth() . I have a list of fitted models and corresponding data. Below is a working example. require(data.table); require(ggplot2) N <- 1000 # Generate some data DT <- NULL models <- list() #

Fitting complex model using Python and lmfit?

我是研究僧i 提交于 2019-12-10 13:37:30
问题 I would like to fit ellipsometric data to complex model using lmfit. Two measured parameters, psi and delta , are variables in a complex function rho . I could try with separating problem to real and imaginary part with shared parameters or picewise approach, but is there any way to do it directly with complex function? Fitting only real part of function works beautifully, but when I define complex residual function I get: TypeError: no ordering relation is defined for complex numbers. Below

Python power law fit with upper limits & asymmetric errors in data using ODR

南笙酒味 提交于 2019-12-04 12:01:08
问题 I'm trying to fit some data to a power law using python. The problem is that some of my points are upper limits, which I don't know how to include in the fitting routine. In the data, I have put the upper limits as errors in y equal to 1, when the rest is much smaller. You can put this errors to 0 and change the uplims list generator, but then the fit is terrible. The code is the following: import numpy as np import matplotlib.pyplot as plt from scipy.odr import * # Initiate some data x = [1

finding the best/ scale/shift between two vectors

不问归期 提交于 2019-12-04 06:15:23
I have two vectors that represents a function f(x), and another vector f(a x+b) i.e. a scaled and shifted version of f(x). I would like to find the best scale and shift factors. *best - by means of least squares error , maximum likelihood, etc. any ideas? for example: f1 = [0;0.450541598502498;0.0838213779969326;0.228976968716819;0.91333736150167;0.152378018969223;0.825816977489547;0.538342435260057;0.996134716626885;0.0781755287531837;0.442678269775446;0]; f2 = [-0.029171964726699;-0.0278570165494982;0.0331454732535324;0.187656956432487;0.358856370923984;0.449974662483267;0.391341738643094;0

Python curve_fit choice of bounds and initial condition affect the result

拥有回忆 提交于 2019-12-03 20:33:35
问题 I have a data set that is described by two free parameters which I want to determine using optimalization.curve_fit . The model is defined as follows def func(x, a, b,): return a*x*np.sqrt(1-b*x) And the fitting part as popt, pcov = opt.curve_fit(f = func, xdata = x_data, ydata= y_data, p0 = init_guess, bounds = ([a_min, b_min], [a_max, b_max])) The outcome of the solutions for a and b depends quite strong on my choice of init_guess , i.e. the initial guess and also on the choice of the

How to estimate the best fitting function to a scatter plot in R?

ε祈祈猫儿з 提交于 2019-12-03 08:47:13
I have scatterplot of two variables, for instance this: x<-c(0.108,0.111,0.113,0.116,0.118,0.121,0.123,0.126,0.128,0.131,0.133,0.136) y<-c(-6.908,-6.620,-5.681,-5.165,-4.690,-4.646,-3.979,-3.755,-3.564,-3.558,-3.272,-3.073) and I would like to find the function that better fits the relation between these two variables. to be precise I would like to compare the fitting of three models: linear , exponential and logarithmic . I was thinking about fitting each function to my values, calculate the likelihoods in each case and compare the AIC values. But I don't really know how or where to start.

Python power law fit with upper limits & asymmetric errors in data using ODR

瘦欲@ 提交于 2019-12-03 07:55:33
I'm trying to fit some data to a power law using python. The problem is that some of my points are upper limits, which I don't know how to include in the fitting routine. In the data, I have put the upper limits as errors in y equal to 1, when the rest is much smaller. You can put this errors to 0 and change the uplims list generator, but then the fit is terrible. The code is the following: import numpy as np import matplotlib.pyplot as plt from scipy.odr import * # Initiate some data x = [1.73e-04, 5.21e-04, 1.57e-03, 4.71e-03, 1.41e-02, 4.25e-02, 1.28e-01, 3.84e-01, 1.15e+00] x_err = [1e-04,

trying to display original and fitted data (nls + dnorm) with ggplot2's geom_smooth()

孤街醉人 提交于 2019-12-03 05:13:31
问题 I am exploring some data, so the first thing I wanted to do was try to fit a normal (Gaussian) distribution to it. This is my first time trying this in R, so I'm taking it one step at a time. First I pre-binned my data: myhist = data.frame(size = 10:27, counts = c(1L, 3L, 5L, 6L, 9L, 14L, 13L, 23L, 31L, 40L, 42L, 22L, 14L, 7L, 4L, 2L, 2L, 1L) ) qplot(x=size, y=counts, data=myhist) Since I want counts, I need to add a normalization factor (N) to scale up the density: fit = nls(counts ~ N *