转 简单多项回归

回归模型比较
部署运行你感兴趣的模型镜像

 例子:销售额x与流通费率y

xx = c(1.5,2.8,4.5,7.5,10.5,13.5,15.1,16.5,19.5,22.5,24.5,26.5)
yy = c(7.0,5.5,4.6,3.6,2.9,2.7,2.5,2.4,2.2,2.1,1.9,1.8)
tt<-data.frame(xx,yy)
names(tt)<-c("x","y")
plot(tt$x,tt$y)

# 直线回归(R2值不理想)
lm.1=lm(y~x,data=tt)
summary(lm.1)

# 多项式回归,假设用二次多项式方程y=a+bx+cx2
lm.2=lm(y~x+I(x^2),data=tt)
summary(lm.2)
plot(tt$x,tt$y)
lines(tt$x,fitted(lm.2))

# 对数法,y=a+blogx
lm.log=lm(y~log(x),data=tt)
summary(lm.log)
plot(tt$x,tt$y)
lines(tt$x,fitted(lm.log))

# 指数法,y=aebx
lm.exp=lm(log(y)~x,data=tt)
summary(lm.exp)
plot(tt$x,tt$y)
lines(tt$x,exp(fitted(lm.exp)))

# 幂函数法,y=axb
lm.pow=lm(log(y)~log(x),data=tt)
summary(lm.pow)
plot(tt$x,tt$y)
lines(tt$x,exp(fitted(lm.pow)))


对比以上各种拟合回归过程得出结论是幂函数法为最佳

您可能感兴趣的与本文相关的镜像

Qwen3-8B

Qwen3-8B

文本生成
Qwen3

Qwen3 是 Qwen 系列中的最新一代大型语言模型,提供了一整套密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面取得了突破性进展

### 多项回归模型的数据分析与预测 #### MATLAB环境下的多项回归应用 在MATLAB环境中,`polyfit`函数用于基于最小二乘法拟合给定阶数的多项式到一组数据点上。对于二次多项式的案例,该过程涉及指定两个参数——横坐标向量(如时间或位置)以及纵坐标向量(如测量值)。接着,通过调用`polyval`函数并传入由`polyfit`返回的系数数组和新的自变量取值范围,可以获得对应的预测结果,进而描绘出平滑的趋势线来表示非线性模式[^1]。 ```matlab % 假设x为独立变量,y为目标变量 p = polyfit(x, y, 2); % 拟合一个二次多项式 y_pred = polyval(p, x); plot(x, y,'o', 'MarkerSize',8); hold on; plot(x, y_pred, '-r','LineWidth',2); title('Data vs Fitted Polynomial'); xlabel('Independent Variable'); ylabel('Dependent Variable'); legend('Observed Data Points','Fitted Curve') ``` #### R语言中的多项回归实践 当采用R语言执行相同任务时,则可借助内置的`lm()`函数配合公式接口完成操作。具体来说,为了构建n次方程形式的关系表达式,可以在右侧加入I()内部定义幂运算或者直接利用多层嵌套的形式书写更高次数项。下面给出了一段简单的例子说明如何创建三次多项回归模型: ```r # 构建三阶多项回归模型 model <- lm(y ~ poly(x, degree=3), data=df) summary(model) # 查看模型摘要信息 newdata <- data.frame(x=seq(min(df$x), max(df$x), length.out=100)) predictions <- predict(model,newdata=newdata) library(ggplot2) ggplot(data=df,aes(x=x))+ geom_point(aes(y=y))+geom_line(aes(y=predictions,color="Predicted"),size=1)+ labs(title='Polynomial Regression Fit', subtitle='Degree of polynomial is set to be three.', caption='Source: Created with ggplot2 package.')+ theme_minimal() ``` #### Python环境下Scikit-Learn库的应用实例 除了上述两种编程平台外,Python同样提供了强大的工具支持此类工作流程。特别是scikit-learn这个开源机器学习库,其中包含了专门处理这类问题的功能模块Pipeline,允许用户轻松地组合预处理器与估计器对象,实现端到端的工作流自动化管理。这里展示了一个完整的代码片段演示怎样运用此框架来进行四阶多项式变换后的线性回归训练: ```python from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn.pipeline import make_pipeline import numpy as np import matplotlib.pyplot as plt def build_poly_regression(degree): """Builds a pipeline that first applies Polynomial Features transformation, followed by fitting linear regression model.""" return make_pipeline(PolynomialFeatures(degree), LinearRegression()) np.random.seed(0) X = np.sort(np.random.rand(50)*10).reshape(-1, 1) y = (X ** 4 + X ** 3 - 7 * X ** 2 + 9*X + np.random.randn(*X.shape)).flatten() degree = 4 model = build_poly_regression(degree) model.fit(X, y) xx = np.linspace(0, 10, 1000).reshape(-1, 1) yy = model.predict(xx) plt.scatter(X, y, label='Training samples') plt.plot(xx, yy, c='red', lw=2, label=f'Order-{degree} fit') plt.legend(loc='best') plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值