curve-fitting

How do I calculate r-squared using Python and Numpy?

不羁的心 提交于 2019-12-17 07:02:29
问题 I'm using Python and Numpy to calculate a best fit polynomial of arbitrary degree. I pass a list of x values, y values, and the degree of the polynomial I want to fit (linear, quadratic, etc.). This much works, but I also want to calculate r (coefficient of correlation) and r-squared(coefficient of determination). I am comparing my results with Excel's best-fit trendline capability, and the r-squared value it calculates. Using this, I know I am calculating r-squared correctly for linear best

Fitting polynomial model to data in R

血红的双手。 提交于 2019-12-17 04:40:05
问题 I've read the answers to this question and they are quite helpful, but I need help particularly in R. I have an example data set in R as follows: x <- c(32,64,96,118,126,144,152.5,158) y <- c(99.5,104.8,108.5,100,86,64,35.3,15) I want to fit a model to these data so that y = f(x) . I want it to be a 3rd order polynomial model. How can I do that in R? Additionally, can R help me to find the best fitting model? 回答1: To get a third order polynomial in x (x^3), you can do lm(y ~ x + I(x^2) + I(x

How to use curvefit in python

佐手、 提交于 2019-12-14 03:08:45
问题 I am studying nonlinear curvefit with python. I made example like below. But the optimized plot is not drawn well plt.plot(basketCont, fittedData) I guess the optimized parametes are not good also. Could you give some recommends? Thank you. import matplotlib matplotlib.use('Qt4Agg') import matplotlib.pyplot as plt from matplotlib.pyplot import cm import numpy as np from scipy.optimize import curve_fit def func(x, a, b, c): return a - b* np.exp(c * x) baskets = np.array([475, 108, 2, 38, 320])

b-splines for drawing, not predicting, based on control path

坚强是说给别人听的谎言 提交于 2019-12-14 01:58:47
问题 I've tried to approach my general problem through two separate questions here on SO: Specify clamped knot vector in bs-call and Fit a B spline to a control path. The responses to these have led me to a reformulation of the bigger problem, which I'm now posting. What I'm trying to accomplish is (in R), given a set of (non-monotonous) control points, how do I draw a b-spline along the path given by the control points. The b-spline must be clamped in both ends. This is purely for graphical

Matlab curve-fitting won't work for small values (1e-12), what can I do?

北战南征 提交于 2019-12-14 01:45:51
问题 I have the Curve Fitting toolbox installed and I'm trying to fit diffusion-data to a specific function. The function is an error-function of the form: y = 3500 - 2500 * erf( ( x-x0 ) / ( 2 * sqrt( D * t )) ) I want the app to give me reasonable values for D and x0 , while t is a predefined constant. The data-points, which the fitting is based on include values for x and y. I know that D should be around 1e-11 and x0 is around 0.0014 but the function won't find these solutions on its own.

Fit and compare multiple sigmoid curves in R

ぃ、小莉子 提交于 2019-12-14 00:27:51
问题 I would like to fit multiple curves at once, and compare them statistically, in terms of their 3 estimated parameters – asymptote, slope and x0. Here is an idealized image of the data to be modeled: Most searchable pages turn up methods to fit a single curve, as seen here: http://kyrcha.info/2012/07/08/tutorials-fitting-a-sigmoid-function-in-r/ and here http://rstudio-pubs-static.s3.amazonaws.com/7812_5327615eb0044cf29420b955ddaa6173.html In my case, I would like to test (statistically) the

Python: Cubic Spline Regression for a time series data

戏子无情 提交于 2019-12-13 23:27:29
问题 I have the data as shown below. I want to find a CUBIC SPLINE curve that fits the entire data set (link to sample data). Things I've tried so far: I've gone through scipy's Cubic Spline Functions, but all of them are only able to give results at a single time only, whereas I want a single curve for the entire time range. I plotted a graph by taking an average of the spline coefficients generated by scipy.interpolate.splrep for a 4 number of knots, but the results were not good and didn't

How to fit a log-normal distribution with Scipy?

微笑、不失礼 提交于 2019-12-13 15:38:01
问题 I want to fit the log-normal parameters mu and sigma to an existing (measured) log-normal distribution. The measured log-normal distribution is defined by the following x and y arrays: x: 4.870000000000000760e-09 5.620000000000000859e-09 6.490000000000000543e-09 7.500000000000000984e-09 8.660000000000001114e-09 1.000000000000000021e-08 1.155000000000000085e-08 1.334000000000000067e-08 1.540000000000000224e-08 1.778000000000000105e-08 2.054000000000000062e-08 2.371000000000000188e-08 2

calculate x-value of curve maximum of a smooth line in R and ggplot2

女生的网名这么多〃 提交于 2019-12-13 08:28:52
问题 data <- dput(data): structure(list(x = 1:16, y = c(-79.62962963, -84.72222222, -88.42592593, -74.07407407, -29.62962963, 51.38888889, 79.62962963, 96.2962963, 87.96296296, 88.42592593, 73.14814815, 12.96296296, -63.42592593, -87.03703704, -87.5, -87.96296296)), .Names = c("x", "y"), row.names = c(NA, 16L), class = "data.frame") I calculated in R with ggplot2 a smooth line for my dataset: p1 <- ggplot(data, aes(x=x(°), y=(%))) library(splines) library(MASS) (p2 <- p1 + stat_smooth(method = "lm

Regressing periodic data with sklearn

流过昼夜 提交于 2019-12-13 05:35:46
问题 I have a dataset with a Regression problem. Earlier i thought it is a linear regression problem but when i plotted "date_time" against "traffic_volume" then it turned out be something like a Sine curve so i decided to go for " Curve Fitting ". Here's the code: import pandas as pd from sklearn.model_selection import train_test_split import numpy as np import datetime as dt from sklearn.linear_model import LinearRegression from sklearn import linear_model from sklearn.model_selection import