请给出5种多元分析统计的代码例子
时间: 2024-10-10 12:02:00 浏览: 14
应用多元统计分析.zip
多元分析是一种统计方法,用于研究多个变量之间的相互影响以及它们与响应变量的关系。以下是五个常见的多元分析相关的Python代码示例,使用pandas、NumPy和sklearn库:
1. **线性回归(Linear Regression)**:
```python
import pandas as pd
from sklearn.linear_model import LinearRegression
# 加载数据
data = pd.read_csv('data.csv')
# 定义特征和目标变量
X = data[['feature1', 'feature2']]
y = data['target']
model = LinearRegression()
model.fit(X, y)
```
2. **主成分分析(PCA)**:
```python
from sklearn.decomposition import PCA
data = pd.read_csv('data.csv')
X = data.drop('target', axis=1)
pca = PCA(n_components=2) # 保留两个主成分
principal_components = pca.fit_transform(X)
```
3. **方差膨胀因子(VIF)检查**:
```python
from statsmodels.stats.outliers_influence import variance_inflation_factor
data = pd.read_csv('data.csv')
vif = pd.DataFrame()
vif["Features"] = data.columns[:-1]
vif["VIF Factor"] = [variance_inflation_factor(data.values, i) for i in range(vif.shape[0])]
```
4. **多元线性判别分析(MLDA)**:
```python
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
lda = LinearDiscriminantAnalysis()
lda.fit(X_train, y_train)
y_pred = lda.predict(X_test)
```
5. **因子分析(Factor Analysis)**:
```python
from factor_analyzer import FactorAnalyzer
fa = FactorAnalyzer(rotation='varimax') # 可选旋转方法
fa.fit(X)
factor_scores = fa.transform(X)
```
阅读全文