研究货运总量 y (万吨)与工业总产值 x1(亿元)、农业总产值 x2(亿元),居民非商品支出 X3 (亿元)的关系。数据见表3-9。
(1)计算出 y , x1 ,x2, x3 的相关系数矩阵。
(2)求 y 关于 x1 ,x2, x3 的三元线性回归方程。
(3)对所求得的方程做拟合优度检验。
(4)对回归方程做显著性检验。
(5)对每一个回归系数做显著性检验。
(6)如果有的回归系数没通过显著性检验,将其剔除,重新建立回归方程归方程的显著性检验和回归系数的显著性检验。
(7)求出每一个回归系数的置信水平为95%的置信区间
8)求标准化回归方程。
(9)求当X01=75,X02=42,X03=3.1时的,给定置信水平为95%,用算精确置信区间,手工计算近似预测区间
(10)结合回归方程对问题做一些基本分析
表3-9

注:每一小问的运行结果我以备注的形式 放在代码段里面
#导入需要的库和数据
import numpy as np
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.stats.api import anova_lm
import matplotlib.pyplot as plt
import pandas as pd
from patsy import dmatrices
# Load data
df = pd.read_csv('C:\\Users\\joyyiyi\\Desktop\\zy3.11.csv',encoding='gbk')
#解决第(!)问
#计算相关系数
cor_matrix = df.corr(method='pearson') # 使用皮尔逊系数计算列与列的相关性
# cor_matrix = df.corr(method='kendall')
# cor_matrix = df.corr(method='spearman')
print(cor_matrix)
'''
结果:
C:\Users\joyyiyi\AppData\Local\Programs\Python\Python39\python.exe C:/Users/joyyiyi/PycharmProjects/pythonProject6/0.py
y x1 x2 x3
y 1.000000 0.555653 0.730620 0.723535
x1 0.555653 1.000000 0.112951 0.398387
x2 0.730620 0.112951 1.000000 0.547474
x3 0.723535 0.398387 0.547474 1.000000
Process finished with exit code 0
'''
#解决第(2)(3)(4)(5)问
result = smf.ols('y~x1+x2+x3',data=df).fit()
#print(result.params) #输出回归系数
print(result.summary())
print("\n")
print(result.pvalues) #输出p值
#
'''
运行结果:
C:\Users\joyyiyi\AppData\Local\Programs\Python\Python39\python.exe C:/Users/joyyiyi/PycharmProjects/pythonProject6/0.py
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.806
Model: OLS Adj. R-squared: 0.708
Method: Least Squares F-statistic: 8.283
Date: Wed, 09 Nov 2022 Prob (F-statistic): 0.0149
Time: 11:15:30 Log-Likelihood: -43.180
No. Observations: 10 AIC: 94.36
Df Residuals: 6 BIC: 95.57
Df Model: 3
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
Intercept -348.2802 176.459 -1.974 0.096 -780.060 83.500
x1 3.7540 1.933 1.942 0.100 -0.977 8.485
x2 7.1007 2.880 2.465 0.049 0.053 14.149
x3 12.4475 10.569 1.178 0.284 -13.415 38.310
==============================================================================
Omnibus: 0.619 Durbin-Watson: 1.935
Prob(Omnibus): 0.734 Jarque-Bera (JB): 0.562
Skew: 0.216 Prob(JB): 0.755
Kurtosis: 1.922 Cond. No. 1.93e+03
==============================================================================
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.93e+03. This might indicate that there are
strong multicollinearity or other numerical problems.
Intercept 0.095855
x1 0.100197
x2 0.048769
x3 0.283510
dtype: float64
Process finished with exit code 0
'''
'''
(2)回答:线性方程:
Y=-348.2802+3.7540x1+7.1007x2+12.4475x3
(3)回答:R方=0.806,调整后R方=0.708
#或者说R=0.806>R0.05(8)=0.632,所以接受原假设,说明x与y有显著的线性关系
#或者说调整后的决定系数为0.708,说明回归方程对样本观测值的拟合程度较好。
(4)回答:做(F检验)
#原假设H0=β1=β2=β3=0
# F=8.283>F0.05(3,6)=4.76,或者说P=0.0149<α=0.05,说明拒绝原假设H0,x与y有显著的线性关系
(5)x1,x2,x3的t值分别为:
t1=1.942<t0.05(8)=1.943或者α=0.100>α=0.05,所以接受原假设,说明x1对y没有显著的影响
t2=2.465>t0.05(8)=1.943或者α=0.049<α=0.05,所以拒绝原假设,说明x1对y有显著的影响
t3=1.178<t0.05(8)=1.943或者α=0.284>α=0.05,所以接受原假设,说明x1对y没有显著的影响
'''
#在第(5)中发现除了x2外其他回归系数都未通过显著性检验,首先剔除x3看看效果
result = smf.ols('y~x1+x2',data=df).fit()
#print(result.params) #输出回归系数
print(result.summary())
print("\n")
print(result.pvalues) #输出p值
'''
运行结果:
C:\Users\joyyiyi\AppData\Local\Programs\Python\Python39\python.exe C:/Users/joyyiyi/PycharmProjects/pythonProject6/0.py
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.761
Model: OLS Adj. R-squared: 0.692
Method: Least Squares F-statistic: 11.12
Date: Wed, 09 Nov 2022 Prob (F-statistic): 0.00672
Time: 11:49:08 Log-Likelihood: -44.220
No. Observations: 10 AIC: 94.44
Df Residuals: 7 BIC: 95.35
Df Model: 2
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
Intercept -459.6237 153.058 -3.003 0.020 -821.547 -97.700
x1 4.6756 1.816 2.575 0.037 0.381 8.970
x2 8.9710 2.468 3.634 0.008 3.134 14.808
==============================================================================
Omnibus: 1.265 Durbin-Watson: 1.895
Prob(Omnibus): 0.531 Jarque-Bera (JB): 0.631
Skew: -0.587 Prob(JB): 0.730
Kurtosis: 2.630 Cond. No. 1.63e+03
==============================================================================
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.63e+03. This might indicate that there are
strong multicollinearity or other numerical problems.
Intercept 0.019859
x1 0.036761
x2 0.008351
dtype: float64
Process finished with exit code 0
'''
#第(6)问回答:
在剔除x3后,回归方程Y=-459.6237+4.6756x1+8.9710x2
的拟合优度R2=0.761,F值=11.12(有所提高),回归系数的P值均小于0.05 因此回归系数均通过显著性t检验
#第(7)问回答:
通过summary()输出的回归结果最右边“[0.025 0.975]”这个位置可以看到
常数项,x1,x2的回归系数置信水平为95%的置信区间分别为:[-821.547,-97.700],[0.381,8.970],[3.134,14.808]
#标准化
dfnorm = (df-df.mean())/df.std()
new = pd.Series({'x1': 4000,'x2': 3300,'x3': 113000,'x4': 50.0,'x5': 1000.0})
newnorm = (new-df.mean())/df.std()
#标准化后构建无截距模型
resultnorm = smf.ols('y~x1+x2',data=dfnorm).fit()
print(resultnorm.summary())
'''
运行结果:
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.761
Model: OLS Adj. R-squared: 0.692
Method: Least Squares F-statistic: 11.12
Date: Fri, 11 Nov 2022 Prob (F-statistic): 0.00672
Time: 22:51:34 Log-Likelihood: -6.5156
No. Observations: 10 AIC: 19.03
Df Residuals: 7 BIC: 19.94
Df Model: 2
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
Intercept -8.327e-17 0.175 -4.75e-16 1.000 -0.415 0.415
x1 0.4792 0.186 2.575 0.037 0.039 0.919
x2 0.6765 0.186 3.634 0.008 0.236 1.117
==============================================================================
Omnibus: 1.265 Durbin-Watson: 1.895
Prob(Omnibus): 0.531 Jarque-Bera (JB): 0.631
Skew: -0.587 Prob(JB): 0.730
Kurtosis: 2.630 Cond. No. 1.12
==============================================================================
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
Process finished with exit code 0
'''
第(8)问:
标准化后的回归方程为:
y=0.4792x1+0.6765x2-8.327e-17
# Load data
df = pd.read_csv('C:\\Users\\joyyiyi\\Desktop\\zy3.11.csv',encoding='gbk')
# print(df)
result = smf.ols('y~x1+x2',data=df).fit()
#标准化
dfnorm = (df-df.mean())/df.std()
new = pd.Series({'x1': 75,'x2': 42})
newnorm = (new-df.mean())/df.std()
#标准化后构建无截距模型
resultnorm = smf.ols('y~x1+x2',data=dfnorm).fit()
#单值预测
predictnorm = resultnorm.predict(pd.DataFrame({'x1': [newnorm['x1']],'x2': [newnorm['x2']]}))
#因为单值预测是基于标准化后的模型,需要对y值还原,y值还原方法:
ypredict = predictnorm*df.std()['y'] + df.mean()['y']
print("ypredict:")
print(ypredict)
#区间
predictions = result.get_prediction(pd.DataFrame({'x1': [75],'x2': [42]}))
print('置信水平为95%,区间预测:')
print(predictions.summary_frame(alpha=0.05))
#近似预测区间:
ylow=267.83-2*np.sqrt(result.scale)
yup=267.83+2*np.sqrt(result.scale)
print(ylow,yup)
'''
运行结果:
C:\Users\joyyiyi\AppData\Local\Programs\Python\Python39\python.exe C:/Users/joyyiyi/PycharmProjects/pythonProject6/回归作业.py
ypredict:
0 267.829001
dtype: float64
置信水平为95%,区间预测:
mean mean_se ... obs_ci_lower obs_ci_upper
0 267.829001 11.782559 ... 204.435509 331.222493
[1 rows x 6 columns]
219.66776823691464 315.99223176308533
Process finished with exit code 0
'''
第(9)问:
y0的预测值为267.829;
y0预测值的置信水平为95%的精确置信区间为:[204.44,331.22],
y0近似预测区间为:[219.67,315.99]
在做这次作业的时候因为不确定答案对不对,参考了csdn的另一位朋友的文章:
R语言之多元线性回归xt3.11_princess yang的博客-CSDN博客_为了研究货运量y与工业总产值x1
这篇写的很好,比我更有条理哦
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。