是极其潦草的学习笔记 w
# Python 篇
# package 的准备
- pandas
- statsmodels
- openpyxl
# 读取文件
- 要指定好文件的路径
- 最好使用
names=['x','y']
来指定说明变量和目标变量。
import pandas as pd | |
file_pass = r"D:\python\data.xlsx" | |
df = pd.read_excel(file_pass, header=0, names=['cost', 'sales']) | |
import statsmodels.formula.api as smf | |
model = smf.ols('sales ~ cost', data=df) | |
result = model.fit() | |
print(result.summary()) |
输出结果为:
OLS Regression Results | |
============================================================================== | |
Dep. Variable: sales R-squared: 1.000 | |
Model: OLS Adj. R-squared: 1.000 | |
Method: Least Squares F-statistic: 1.981e+31 | |
Date: Tue, 02 May 2023 Prob (F-statistic): 2.50e-47 | |
Time: 14:12:47 Log-Likelihood: 153.61 | |
No. Observations: 5 AIC: -303.2 | |
Df Residuals: 3 BIC: -304.0 | |
Df Model: 1 | |
Covariance Type: nonrobust | |
============================================================================== | |
coef std err t P>|t| [0.025 0.975] | |
------------------------------------------------------------------------------ | |
Intercept 10.0000 1.49e-14 6.71e+14 0.000 10.000 10.000 | |
cost 2.0000 4.49e-16 4.45e+15 0.000 2.000 2.000 | |
============================================================================== | |
Omnibus: nan Durbin-Watson: 0.667 | |
Prob(Omnibus): nan Jarque-Bera (JB): 0.375 | |
Skew: -0.344 Prob(JB): 0.829 | |
Kurtosis: 1.847 Cond. No. 77.8 | |
============================================================================== |
# Stata 篇
# 简单使用 Stata
- Log in
- import data
- save as
- use data
use D:\python\data_回归分析.dta | |
regress 売上高万円 広告費 |
** 注意:** 使用的数据文件一定是.dta 格式,<u> 而这个必须要先导入数据后保存再得到。</u>
输出:
-------------+---------------------------------- F(1, 3) = .
Model | 4000 1 4000 Prob > F = .
Residual | 0 3 0 R-squared = 1.0000
-------------+---------------------------------- Adj R-squared = 1.0000
Total | 4000 4 1000 Root MSE = 0
------------------------------------------------------------------------------
売上高万円 | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
広告費 | 2 . . . . .
_cons | 10 . . . . .
------------------------------------------------------------------------------
# 附录
cost | sales |
---|---|
10 | 30 |
20 | 50 |
30 | 70 |
40 | 90 |
50 | 110 |