我在玩这个代码,这是一元线性混合效应建模。数据集表示:学生作为s

讲师(d)

部门为部门

服务即服务

在R的lme4包语法中(Bates等人,2015),实现的模型可以概括为:y ~ 1 + (1|students) + (1|instructor) + (1|dept) + service

其中1表示截距项,(1 | x)表示x的随机效应,x表示固定效应。在

^{pr2}$

s_train = train['s'].values

d_train = train['dcodes'].values

dept_train = train['deptcodes'].values

y_train = train['y'].values

service_train = train['service'].values

n_obs_train = train.shape[0]

s_test = test['s'].values

d_test = test['dcodes'].values

dept_test = test['deptcodes'].values

y_test = test['y'].values

service_test = test['service'].values

n_obs_test = test.shape[0]

n_s = max(s_train) + 1 # number of students

n_d = max(d_train) + 1 # number of instructors

n_dept = max(dept_train) + 1 # number of departments

n_obs = train.shape[0] # number of observations

# Set up placeholders for the data inputs.

s_ph = tf.placeholder(tf.int32, [None])

d_ph = tf.placeholder(tf.int32, [None])

dept_ph = tf.placeholder(tf.int32, [None])

service_ph = tf.placeholder(tf.float32, [None])

# Set up fixed effects.

mu = tf.get_variable("mu", [])

service = tf.get_variable("service", [])

sigma_s = tf.sqrt(tf.exp(tf.get_variable("sigma_s", [])))

sigma_d = tf.sqrt(tf.exp(tf.get_variable("sigma_d", [])))

sigma_dept = tf.sqrt(tf.exp(tf.get_variable("sigma_dept", [])))

# Set up random effects.

eta_s = Normal(loc=tf.zeros(n_s), scale=sigma_s * tf.ones(n_s))

eta_d = Normal(loc=tf.zeros(n_d), scale=sigma_d * tf.ones(n_d))

eta_dept = Normal(loc=tf.zeros(n_dept), scale=sigma_dept * tf.ones(n_dept))

yhat = (tf.gather(eta_s, s_ph) +

tf.gather(eta_d, d_ph) +

tf.gather(eta_dept, dept_ph) +

mu + service * service_ph)

y = Normal(loc=yhat, scale=tf.ones(n_obs))

#Inference

q_eta_s = Normal(

loc=tf.get_variable("q_eta_s/loc", [n_s]),

scale=tf.nn.softplus(tf.get_variable("q_eta_s/scale", [n_s])))

q_eta_d = Normal(

loc=tf.get_variable("q_eta_d/loc", [n_d]),

scale=tf.nn.softplus(tf.get_variable("q_eta_d/scale", [n_d])))

q_eta_dept = Normal(

loc=tf.get_variable("q_eta_dept/loc", [n_dept]),

scale=tf.nn.softplus(tf.get_variable("q_eta_dept/scale", [n_dept])))

latent_vars = {

eta_s: q_eta_s,

eta_d: q_eta_d,

eta_dept: q_eta_dept}

data = {

y: y_train,

s_ph: s_train,

d_ph: d_train,

dept_ph: dept_train,

service_ph: service_train}

inference = ed.KLqp(latent_vars, data)

这在线性混合效应建模的单变量情况下很好地工作。我试图把这种方法推广到多变量的情况。任何想法都是非常受欢迎的。在

python混合线性模型_Python中的多元线性混合效应模型相关推荐

  1. python 核密度估计_Python中的多元核密度估计

    有几种方法可以在3D中可视化结果 最简单的方法是在用于生成高斯KDE的点上对其求值,然后根据密度估计值对点上色. 例如:import numpy as np from scipy import sta ...

  2. 基于jupyter notebook的python编程-----利用梯度下降算法求解多元线性回归方程,并与最小二乘法求解进行精度对比

    基于jupyter notebook的python编程-----利用梯度下降算法求解多元线性回归方程,并与最小二乘法求解进行精度对比目录 一.梯度下降算法的基本原理 1.梯度下降算法的基本原理 二.题 ...

  3. python map用法_Python中ChainMap的一种实用用法

    Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 简而言之ChainMap:将多个字典视为一个,解锁Python超能力. Python标准库中的集合模块包含 ...

  4. python基本统计量_Python中简单统计量的计算

    本篇文章给大家带来的内容是关于Python中简单统计量的计算,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 1.这些操作都要确保已经在电脑中安装好了Anaconda集成库,如果安装好 ...

  5. python解析原理_Python 中 -m 的典型用法、原理解析与发展演变

    在命令行中使用 Python 时,它可以接收大约 20 个选项(option),语法格式如下: python [-bBdEhiIOqsSuvVWx?] [-c command | -m module- ...

  6. python正则表达式空格_python中的正则表达式的使用

    一.正则表达式简介 正则表达式:又称正规表示式.正规表示法.正规表达式.规则表示式.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或者是RE),是计算 ...

  7. python wraps模块_python中 wraps 的作用

    这里使用两段代码比较加入wraps装饰器后,函数打印的结果对比: 新建文件名:Testword 代码1:不加wraps装饰器 # coding=utf-8 from functools import ...

  8. 用python输出数组_Python 中输出数组中的全部元素

    只需要在开头加上一句即可 import numpy as np np.set_printoptions(threshold=np.inf) Python 输出到文本中 参考:https://blog. ...

  9. python 数据结构转换层_python – 具有Maxpooling1D和channel_first的Keras模型

    我目前尝试在Keras中为时间序列分类构建顺序模型时遇到问题.我想使用channels_first数据,因为从处理的角度来看它更方便(不过我只使用一个通道).这适用于我正在使用的Convolution ...

  10. python 多元线性回归_Python机器学习,多元线性回归分析问题

    @Author:润森,Runsen,公众号:润森笔记 什么是多元线性回归 在回归分析中,如果有两个或两个以上的自变量,就称为多元回归.**事实上,一种现象常常是与多个因素相联系的,由多个自变量的最优组 ...

最新文章

  1. 编译原理-First集和Follow集
  2. 模拟摇奖:从1-36中随机抽出8个不重复的数字
  3. c++几种STL比较
  4. 情感分析(支持向量机,逻辑回归)
  5. kindeditor 限制上传图片大小及宽高
  6. python flask和django_真正搞明白Python中Django和Flask框架的区别
  7. Meteor:用户账号管理添加密码和微博weibo账号系统支持
  8. 量化指标公式源码_通达信博尔量化指标 看了就明白 源码
  9. win10 动态磁盘 linux,大师教你win10系统提示动态磁盘无效的详尽处理办法
  10. 计算机论文鉴定表指导教师评语,毕业论文指导老师评语|毕业论文鉴定表评语...
  11. Microsoft软件保护平台服务一直占用大量CPU资源
  12. JAVA教程--书的
  13. chrome浏览器模拟慢网速环境
  14. 《数据结构》课程设计报告
  15. Windows安装和使用binwalk
  16. 微信实名认证怎么更改?更改微信实名认证只要3个小步骤
  17. 订单测试点(欢迎补充)
  18. JVM 01 走进JVM
  19. 解决不良贷款清收难 创新新对策
  20. 求贤若渴!白鲸开源研发、运营、人力岗虚位以待

热门文章

  1. SQL 注入防御方法总结
  2. 《Objective-C基础教程》学习笔记第九-十章
  3. Filter过滤器实现登录权限拦截
  4. Power Apps注册账户
  5. GrandTotal for Mac v7.2.2 发票收据预算设计软件
  6. python转bat_bat 转换为python
  7. GB28181国标流媒体服务(LiveGBS)-支持海康8700等联网网关通过接入实现web端无插件直播...
  8. Python基础之爬取豆瓣图书信息
  9. 幼儿园绘本图书借阅小程序
  10. Java基础知识面试题(2022版)