lmer(来自 R 包 lme4)如何计算对数似然?

2024-05-14

我试图理解 lmer 函数。我发现了很多关于如何使用该命令的信息,但关于它实际执行的操作的信息却很少(除了这里的一些神秘注释:http://www.bioconductor.org/help/course-materials/2008/PHSIntro/lme4Intro-handout-6.pdf http://www.bioconductor.org/help/course-materials/2008/PHSIntro/lme4Intro-handout-6.pdf)。我正在玩以下简单的例子:

library(data.table)
library(lme4)
options(digits=15)

n<-1000
m<-100
data<-data.table(id=sample(1:m,n,replace=T),key="id")
b<-rnorm(m)
data$y<-rand[data$id]+rnorm(n)*0.1
fitted<-lmer(b~(1|id),data=data,verbose=T)
fitted

我知道 lmer 正在拟合 Y_{ij} = beta + B_i + epsilon_{ij} 形式的模型,其中 epsilon_{ij} 和 B_i 是独立法线,分别具有方差 sigma^2 和 tau^2。如果 theta = tau/sigma 是固定的,我用正确的均值和最小方差计算出 beta 的估计值

c = sum_{i,j} alpha_i y_{ij}

where

alpha_i = lambda/(1 + theta^2 n_i)
lambda = 1/[\sum_i n_i/(1+theta^2 n_i)]
n_i = number of observations from group i

I also computed the following unbiased estimate for sigma^2:

s^2 = \sum_{i,j} alpha_i (y_{ij} - c)^2 / (1 + theta^2 - lambda)

这些估计似乎与 lmer 的结果一致。但是,我无法弄清楚在这种情况下如何定义对数似然。我计算出概率密度为

pd(Y_{ij}=y_{ij}) = \prod_{i,j}[f_sigma(y_{ij}-ybar_i)]
    * prod_i[f_{sqrt(sigma^2/n_i+tau^2)}(ybar_i-beta) sigma sqrt(2 pi/n_i)]

where

ybar_i = \sum_j y_{ij}/n_i (the mean of observations in group i)
f_sigma(x) = 1/(sqrt{2 pi}sigma) exp(-x^2/(2 sigma)) (normal density with sd sigma)

但上面的日志不是 lmer 产生的。在这种情况下如何计算对数似然(对于奖励分数,为什么)?

Edit:更改了一致性符号,删除了标准偏差估计的错误公式。


评论中的链接包含了答案。下面我将公式简化后的内容放在这个简单的示例中,因为结果有些直观。

lmer fits a model of the form Y_{ij} = \beta + B_i + \epsilon_{ij}, where \epsilon_{ij} and B_i are independent normals with variances \sigma^2 and \tau^2 respectively. The joint probability distribution of Y_{ij} and B_i is therefore

where

f_{\sigma^2}(x)=\frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{x^2}{2\sigma^2}}.

The likelihood is obtained by integrating this with respect to b_i (which isn't observed) to give

where n_i is the number of observations from group i, and \bar y_i is the mean of observations from group i. This is somewhat intuitive since the first term captures spread within each group, which should have variance \sigma^2, and the second captures the spread between groups. Note that \sigma^2/n_i+\tau^2 is the variance of \bar y_i.

However, by default (REML=T) lmer maximises not the likelihood but the "REML criterion", obtained by additionally integrating this with respect to \beta to give

where \hat\beta is given below.

最大化似然 (REML=F)

If \theta=\tau/\sigma is fixed, we can explicitly find the \beta and \sigma which maximise likelihood. They turn out to be

Note \hat\sigma^2 has two terms for variation within and between groups, and \hat\beta is somewhere between the mean of y_{ij} and the mean of \bar y_i depending on the value of \theta.

Substituting these into likelihood, we can express the log likelihood l in terms of \theta only:

lmer iterates to find the value of \theta which minimises this. In the output, -2l and l are shown in the fields "deviance" and "logLik" (if REML=F) respectively.

最大化限制似然 (REML=T)

Since the REML criterion doesn't depend on \beta, we use the same estimate for \beta as above. We estimate \sigma to maximise the REML criterion:

The restricted log likelihood l_R is given by

In the output of lmer, -2l_R and l_R are shown in the fields "REMLdev" and "logLik" (if REML=T) respectively.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

lmer(来自 R 包 lme4)如何计算对数似然? 的相关文章

  • 如何从 ISI Web of Knowledge 检索有关期刊的信息?

    我正在从事一些预测文章引用计数的工作 我遇到的问题是我需要 ISI Web of Knowledge 中有关期刊的信息 他们逐年收集这些信息 期刊影响因子 特征因子 但无法一次下载所有一年期期刊信息 只有 标记全部 选项 该选项始终标记列表
  • 有没有一种简单的方法可以在 R 的 igraph 中按度数对网络节点进行着色?

    使用igraphR 包 我想按度数对网络节点进行着色 颜色应代表渐变 例如从蓝色到红色 或从黄色到红色 从网络中观察到的最低程度到最高程度 我找到了一个可行的解决方案 https stackoverflow com questions 40
  • 删除第一次出现某个值后的行

    我有以下 df df lt data frame var1 c 1 2 2 3 4 5 5 6 7 8 9 var2 c a b c d e f g h i j k 我想在达到前 5 var1 后进行过滤 所以结果应该是 var1 var2
  • 基于两列对数据框中的行进行求和[重复]

    这个问题在这里已经有答案了 我想添加一列的值 将它们按两列分组 我找到了如何在一列上执行此操作 但无法弄清楚如何在两列上执行此操作 例如 如果我有以下数据框 x c a a b b c c a a b b c c a a b b c c y
  • 如何扩展 ggplot y 轴限制以包含最大值

    通常 在图中 Y 轴值标签会在绘制的最大值下方被截断 For example library tidyverse mtcars gt ggplot aes x mpg y hp geom point 我知道scale y continous
  • 计算横截面积作为高度的函数

    我试图弄清楚如何计算不同水位的河流横截面的充满水的面积 对于横截面 我有 5 m 宽河流上每 25 cm 的深度 并且可以根据之前很好回答的问题来计算面积计算不同高度的横截面积 https stackoverflow com questio
  • 将 Excel 数字日期重新格式化为 R 日期

    希望将从 Excel 中提取的列重新格式化为包含数字 Excel 格式 例如 40182 的数据框 as date 40182 origin 1899 12 30 format b Y Returns 1 2070 年 1 月 5 日 我正
  • 将 SAS sas7bdat 数据读入 R

    R 有哪些选项可以读取本机 SAS 格式的文件 sas7bdat 进入R The NCES 共同核心 https nces ed gov ccd pubschuniv asp例如 包含以此格式保存的大量数据文件存储库 为了具体起见 让我们集
  • 写入抓取数据的 csv 文件时如何拆分项目名称

    我有兴趣使用 R 从网上抓取的数据创建 csv 或类似的 Excel 兼容文件 到目前为止 我通过执行以下操作来存储数据 require textreadr spiegel lt read html http www spiegel de
  • R:从 Github 安装包时出现编码问题

    我正在尝试安装dcStockR https github com yutannihilation dcStockR来自 Github 的包 这是一个htmlwidgets http www htmlwidgets org 周围的包装纸dc
  • 获取数据集 R 包中所有对象名称的列表?

    如何获取对象中对象的确切名称列表datasets https stat ethz ch R manual R devel library datasets html 00Index html包裹 我在这里找到了很多 data package
  • 如何通过在R闪亮循环中读取.csv文件来动态生成dataTableOutput?

    我有一个函数可以生成 n 个数据帧并将其作为 csv 文件保存在某个位置 并且该函数返回已保存 CSV 的文件名 我希望获取这些 csv 文件 使用以下命令读取它read csv 然后使用 renderUI 和 renderDataTabl
  • 闪亮的传单添加大量分离的折线

    我有一个 200k 行数据集 其中包含出发地和目的地的坐标 我有一个 R 闪亮的应用程序 带有传单地图 可以在这些坐标上显示圆圈 尽管坐标数量很大 但效果很好 这是数据的简化示例 每行包含出行id 出发地经纬度 目的地经纬度 id lat
  • 为什么表达式“1”==1 的计算结果为 TRUE? [复制]

    这个问题在这里已经有答案了 1 是字符值 其他1是数字 甚至 当我尝试在下面执行时 它给了我 TRUE as character 0 as numeric 0 谁能帮助我理解 为什么 来自help 如果两个参数是不同类型的原子向量 则其中一
  • R:适合显示具有倾斜计数的数据的图

    我有这样的数据 Name Count Object1 110 Object2 111 Object3 95 Object4 40 Object2000 1 因此 只有前 3 个物体的计数较高 其余 1996 个物体的数量少于 40 个 其中
  • 使用 xtable 对乳胶输出的表进行排序

    我正在尝试生成一个排序表并导出到乳胶中 然而 xtable 似乎无法处理排序表 建议 a lt sample letters 500 replace T b lt table a c lt sort table a decreasing T
  • 省略 RColorBrewer 调色板上较亮的颜色以在 ggplot2 中使用

    我想在 RColorBrewer 的 Oranges 调色板中使用较深的颜色 以便在我的 ggplot 条形图 中使用 然而我却做不到 帮助 下面是示例代码 my palette brewer pal n 9 Oranges 4 9 Bar
  • 如何在复杂的皂膜GAM中设置更平滑的边界条件?

    我正在对南太平洋岛屿泻湖中宽吻海豚的分布进行建模 我想使用肥皂膜平滑器来模拟海豚在二维表面 经度 x 纬度 上存在的概率 考虑到陆地边界 显然海豚不能在陆地上行走 我想知道如何将我的研究区域 陆地和近海水域 的边界固定为等于零的条件 因为我
  • 二部图匹配以匹配两个集合

    我是新手igraphR 中的包 我有两套A and B 每个都有N顶点 A1 A2 AN and B1 B2 BN 每个元素之间都有一个边缘A对每一个元素B 我有一个函数fWgt Ai Bj 返回之间的边的权重Ai and Bj 我一直在尝
  • 使用 alpha 通道叠加两个 ggplot2 stat_密度2d 图

    我想叠加两个ggplot2使用 alpha 通道进行绘图 结果图像显示两个数据集 这是我的测试数据 data read table text P1 1 0 4 nP2 0 0 2 nP3 2 1 8 nP4 2 2 6 nP5 0 5 2

随机推荐