请选择 进入手机版 | 继续访问电脑版
楼主: HelianthusLucy
1714 5

[其他] 【回归分析求助】stata分位数回归中如何用lasso算法? [推广有奖]

  • 0关注
  • 3粉丝

院士

3%

还不是VIP/贵宾

-

威望
0
论坛币
13470 个
通用积分
92.3636
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
9718 点
帖子
1262
精华
0
在线时间
959 小时
注册时间
2018-3-27
最后登录
2024-3-23

100论坛币
请问stata分位数回归中可以用lasso进行变量选择吗?如果有的话该敲什么代码??在网上只找到了lassopack这个包,但是似乎只能做OLS的Lasso。

最佳答案

love提 查看完整内容

import numpy as np import matplotlib.pyplot as plt from sklearn.ensemble import GradientBoostingRegressor %matplotlib inline np.random.seed(1) #设置随机数生成的种子 def f(x): """The function to predict.""" return x * np.sin(x) #对x取正弦 #---------------------------------------------------------------------- # First the noiseless case X = np.atleast_2d(np.random.unifor ...
关键词:LASSO 分位数回归 Stata 回归分析 分析求助
不忘初心
love提 发表于 2020-9-21 19:34:38 |显示全部楼层 |坛友微信交流群
import numpy as np
import matplotlib.pyplot as plt

from sklearn.ensemble import GradientBoostingRegressor
%matplotlib inline
np.random.seed(1)
#设置随机数生成的种子


def f(x):
    """The function to predict."""
    return x * np.sin(x)
#对x取正弦

#----------------------------------------------------------------------
#  First the noiseless case
X = np.atleast_2d(np.random.uniform(0, 10.0, size=100)).T
#  随机采样并转换成数组
#  numpy.atleast_2d()其他格式转换成数组
#  numpy.random.uniform(low,high,size) 从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high.
#  .T 数组转置.
X = X.astype(np.float32)
#转换数据类型 float32 减少精度

# Observations
y = f(X).ravel()
#多维数组转1维向量

dy = 1.5 + 1.0 * np.random.random(y.shape)
#生成 np.random.random(y.shape) y.shape大小矩阵的随机数


noise = np.random.normal(0, dy)
#生成一个正态分布,正态分布标准差(宽度)为dy

y += noise
y = y.astype(np.float32)

# Mesh the input space for evaluations of the real function, the prediction and
# its MSE
xx = np.atleast_2d(np.linspace(0, 10, 1000)).T
#np.linespace 产生从0到10,1000个等差数列中的数字

xx = xx.astype(np.float32)

alpha = 0.95

clf = GradientBoostingRegressor(loss='quantile', alpha=alpha,
                                n_estimators=250, max_depth=3,
                                learning_rate=.1, min_samples_leaf=9,
                                min_samples_split=9)
#loss: 选择损失函数,默认值为ls(least squres)

# learning_rate: 学习率,
# alpha ,quantile regression的置信度

# n_estimators: 弱学习器的数目,250

# max_depth: 每一个学习器的最大深度,限制回归树的节点数目,默认为3

# min_samples_split: 可以划分为内部节点的最小样本数,9

# min_samples_leaf: 叶节点所需的最小样本数,9

clf.fit(X, y)

# Make the prediction on the meshed x-axis
y_upper = clf.predict(xx)
#用训练好的分类器去预测xx

clf.set_params(alpha=1.0 - alpha)
#1-0.95=0.05 再训练一条
clf.fit(X, y)

# Make the prediction on the meshed x-axis
y_lower = clf.predict(xx)

clf.set_params(loss='ls')
#改成 ls :least squares regression 最小二乘回归
clf.fit(X, y)

# Make the prediction on the meshed x-axis
y_pred = clf.predict(xx)

# Plot the function, the prediction and the 95% confidence interval based on
# the MSE
#matplot画图   y_upper(alpha=0.95)画一条   y_lower(alpha=0.05)画一条  普通的线性回归(prediction)画一条
fig = plt.figure()
plt.plot(xx, f(xx), 'g:', label=r'$f(x) = x\,\sin(x)$')
plt.plot(X, y, 'b.', markersize=10, label=u'Observations')
plt.plot(xx, y_pred, 'r-', label=u'Prediction')
plt.plot(xx, y_upper, 'k-')
plt.plot(xx, y_lower, 'k-')
plt.fill(np.concatenate([xx, xx[::-1]]),
         np.concatenate([y_upper, y_lower[::-1]]),
         alpha=.5, fc='b', ec='None', label='95% prediction interval')
plt.xlabel('$x$')
plt.ylabel('$f(x)$')
plt.ylim(-10, 20)
plt.legend(loc='upper left')
plt.show()

使用道具

18530531926 发表于 2020-9-22 16:25:33 |显示全部楼层 |坛友微信交流群
你去求求数学大佬

使用道具

love提 发表于 2020-9-25 13:37:22 |显示全部楼层 |坛友微信交流群
运行结果
附件: 你需要登录才可以下载或查看附件。没有帐号?我要注册

使用道具

小旭旭555 发表于 2021-4-3 13:00:31 来自手机 |显示全部楼层 |坛友微信交流群
love提 发表于 2020-9-21 19:34
import numpy as np
import matplotlib.pyplot as plt

你好,请问截面数据做分位数回归需不需要做wald检验

使用道具

806966428 发表于 2023-3-19 11:44:42 |显示全部楼层 |坛友微信交流群
解决了吗

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-16 13:56