线性线性混合效应模型及R语言实现

线性线性混合效应模型及R语言实现1.Averybasictutorialforperforminglinearmixedeffectsanalyses(入门极品)Tutorial1: http://www.bodowinter.com/tutorial/bw_LME_tutorial1.pdfTutorial2: http://www.bodowinter.com/tutorial/bw_LME_t…

1.A very basic tutorial for performing linear mixed effects analyses(入门极品)

Tutorial 1: http://www.bodowinter.com/tutorial/bw_LME_tutorial1.pdf

Tutorial 2: http://www.bodowinter.com/tutorial/bw_LME_tutorial.pdf

nlme (Non-Linear Mixed Effects), lme4 (Linear Mixed Effects) and asreml (average spatial real)

 

2.其它资料(Google搜索即有)

Mixed-Eects Models in R (较好)

An Appendix to An R Companion to Applied Regression, Second Edition

 

John Fox & Sanford Weisberg

Linear Mixed-Effects Models Using R(一本教材,进阶选用)

 

A Step-by-Step Approach

 

Andrzej Ga?ecki ? Tomasz Burzykowski

 

3.R中的线性混合模型介绍(简单了解不同的包)

http://blog.sciencenet.cn/blog-2577109-949820.html

https://www.r-bloggers.com/linear-mixed-models-in-r/

 

3.语法备忘

 

  • 三种模型:
  • AOD固定斜率,DAY随机截距:LMM.model = lmer(PM25 ~ AOD + (1|Day) , data=LMMexcdata)
  • AOD随机斜率,DAY固定截距:LMM.model3 = lmer(PM25 ~ AOD + (0 + AOD|Day) , data=LMMexcdata)
  • AOD随机斜率,DAY随机截距:LMM.model2 = lmer(PM25 ~ AOD + (1 + AOD|Day) , data=LMMexcdata)

 

  • 师姐发来的查看斜率和截距的程序:

sslopef=as.numeric(as.matrix(lme4::fixef(fm1)[2]))

sloper=as.numeric(LMM.model(lme4::ranef(fm1)$Day[,2]))

intersf= as.numeric(LMM.model(lme4::fixef(fm1)[1]))

intersr=as.numeric(LMM.model(lme4::ranef(fm1)$Day[,1]))

 

lme4::fixef(yourmodle)读取固定截距和斜率

lme4::fixef(yourmodle)读取随机截距和斜率

 

文档中示例(A very basic tutorial for performing linear mixed effects analyses):

-----------------------------------------

#load data into R

politeness=read.csv(“http://www.bodowinter.com/tutorial/politeness_data.csv”)

#================view the dataset======================

#show the head of politeness

head(politeness)

#show the tail of politeness

tail(politeness)

# other commands you may use

summary(politeness)

str(politeness)

colnames(politeness)

#================check for missing values======================

which(!complete.cases(politeness))

#======look at the relationship between politeness and pitch by means of a boxplot==========

boxplot(frequency ~ attitude*gender,

        col=c(“white”,”lightgray”),politeness)

#================A random intercept model======================

library(Matrix)

library(lme4)

lmer(frequency ~ attitude, data=politeness)

politeness.model = lmer(frequency ~ attitude +

                          gender + (1|subject) +

                          (1|scenario), data=politeness)

summary(politeness.model)

#Statistical significance test

politeness.null = lmer(frequency ~ gender +

                         (1|subject) + (1|scenario), data=politeness,

                       REML=FALSE)

politeness.model = lmer(frequency ~ attitude +

                          gender + (1|subject) + (1|scenario),

                        data=politeness, REML=FALSE)

anova(politeness.null,politeness.model)

#look at the coefficients of the model by subject and by item

coef(politeness.model)

#================A random slope model======================

politeness.model = lmer(frequency ~ attitude +

                          gender + (1+attitude|subject) +

                          (1+attitude|scenario),

                        data=politeness,

                        REML=FALSE)

coef(politeness.model)

#Statistical significance test (try to obtain a p-value)

#construct the null model

politeness.null = lmer(frequency ~ gender +

                         (1+attitude|subject) + (1+attitude|scenario),

                       data=politeness, REML=FALSE)

#do the likelihood ratio test

anova(politeness.null,politeness.model)

-----------------------------------------------------

结果分析

1.使用数据:subject(F1,F2,F3,M3,M4,M7),gender(M,F),scenario 语(1~7),attitude(inf,pol),frequency(e.g.200Hz)

线性线性混合效应模型及R语言实现 - 影 - 我的博客

 

2.随机截距模型:

2.1 代码

politeness.model = lmer(frequency ~ attitude + gender + (1|subject) + (1|scenario), data=politeness, REML=FALSE)

其中:

Fix effect: attitude, gender(固定斜率)

Random intercepts: subject, scenario (随机截距)

*we’ve told the model with “(1|subject)” and “(1|scenario)” to take by-subject and by-item variability into account.

2.2 输出系数分析:(注:这个系数是总系数,固定系数和随机系数已加和)

--------------------

PS. 若想分别查看固定系数和随机系数,使用以下命令

#输出固定效应系数

print(lme4::fixef(yourmodel))

#输出随机效应系数

print(lme4::ranef(yourmodel))

------------------

线性线性混合效应模型及R语言实现 - 影 - 我的博客

对于随机截距模型:

Random intercepts:

scenario1~7:各自具有不同的随机截距

subject(F1,F2,F3,M3,M4,M7):各自具有不同的随机截距

Fix effect:

对于所有项都是相同的:attitudepol :attitudepol的pol斜率

                                        genderM:geder的M斜率 

 

3.随机截距模型+随机截距模型

3.1 代码

politeness.model = lmer(frequency ~ attitude + gender + (1+attitude|subject) + (1+attitude|scenario), data=politeness, REML=FALSE)

其中:

> Fix effect: 

gender为固定斜率,

> Radom effect:

让attitude随机斜率,

subject,scenario为随机截距

*The notation “(1+attitude|subject)” means that you tell the model to expect differing baseline-levels of frequency (the intercept, represented by 1)

 

3.2 输出系数分析:(注:这个系数是总系数,固定系数和随机系数已加和)

线性线性混合效应模型及R语言实现 - 影 - 我的博客

 

> Fix effect:

genderM:geder的M斜率,固定斜率

> Random intercepts:

scenario1~7:各自具有不同的随机截距

subject(F1,F2,F3,M3,M4,M7):各自具有不同的随机截距

attitudepol :attitudepol的pol斜率,随机斜率

 

--------------------------------------------

自己写的程序:

(读取nc文件并合并,做混合效应模型+系数验证)

#========Set the working directory (on a Mac) and load the data========

##setwd(“/Users/Gewanru/Documents/mix effect/”)

#=======Read Data from a folder(循环读入多个txt文件)==========

##a = list.files(“input”)

##dir = paste(“./input/”,a,sep=””)            

##n = length(dir)     

##LMMexcdata = read.table(file = dir[1],header=TRUE,dec = “.”)

##for (i in 2:n){

##  new.data = read.table(file = dir[i], header=TRUE, dec = “.”)

##  LMMexcdata = rbind(LMMexcdata,new.data)

##}

#read a single table

##LMMexcdata <- read.table(file = “merge.txt”, header = TRUE, dec = “.”)

#========view the data set(查看数据结构)=============

#see the variable names of the data 

##names(LMMexcdata)

#show the head of the data

##head(LMMexcdata)

#show the data structure

##str(LMMexcdata)

#check for missing values(misiing value:”NA”)

##which(!complete.cases(LMMexcdata))

#show the tail of the data

##tail(LMMexcdata)

# other commands may use

##summary(LMMexcdata)

##colnames(LMMexcdata)

#==========Read nc files(读取气象nc文件,若不用则用上一个读取方式)===============

setwd(“/Users/Gewanru/Documents/mix effect/2015/Resultsnc”)

library(ncdf4)

ncname <- “0101”

ncfname <- paste(ncname, “.nc”, sep = “”)

dname <- “PM25”

#===open a NetCDF file==

ncin <- nc_open(ncfname)

print(ncin) #Obtain some basic information of the nc file

#===Read the data from a variable in the file==

PM25data <- ncvar_get(ncin)

nc_close(ncin)

dim(PM25data)#show the the size of the array

print(PM25)

#=======Drawing some plots=============

#Scatter plot(散点图)

##x <- LMMexcdata$AOD #define x lab

##y <- LMMexcdata$PM25 #define y lab

##plot(x, y, main = “Title”, xlab = “PM2.5”, ylab = “AOD”) # “” : mian title & Title of x(y)lab

#Box plot(箱线图)

#

#====================Correlation(求相关)=========================

#(Here we need to use all of the data)

#correlation

x <- LMMexcdata[,c(“PM25″,”AOD”)]

y <- LMMexcdata[,c(“PM25″,”AOD”)]

cor(x,y)

#correlation & Significance test(问题1:怎么让输出的数据位数更多一点?)

library(mnormt)

library(psych)

x <- LMMexcdata[,c(“PM25″,”AOD”)]

y <- LMMexcdata[,c(“PM25″,”AOD”)]

corr.test(x,y,use = “complete”)

#===================== Linear Modle (线性回归模型)=====================

#The simplest modle(without temporal and spatial variations)

LMsimplest.model = lm(PM25 ~ AOD , data=LMMexcdata)

summary(LMsimplest.model)

coef(LMsimplest.model)

#Get the coefficient of the Linear Modle

#c1 <- LMsimplest.model$coefficient

#===========Linear Mixed effects model (混合效应模型)===================

#1.(Without site effect)

library(Matrix)

library(lme4)

LMM.model = lmer(PM25 ~ AOD + (1 + AOD|Day) , data=LMMexcdata)

summary(LMM.model)

coef(LMM.model)

print(lme4::fixef(LMM.model))

print(lme4::ranef(LMM.model))

 

#2.(With site effect)

##LMMsite.model = lmer(PM25 ~ AOD + (1 + AOD|Day) + (1|Site) , data=LMMexcdata)

##summary(LMMsite.model)

##coef(LMMsite.model)

#输出固定效应系数

##print(lme4::fixef(LMMsite.model))

#输出随机效应系数

##print(lme4::ranef(LMMsite.model))

 

#============Significance test of the model(混合效应模型显著性检验)============

LMM.null1 = lmer(PM25 ~ AOD + (0 + AOD|Day) , data=LMMexcdata, REML=FALSE)

LMM.null2 = lmer(PM25 ~ AOD + (1|Day) , data=LMMexcdata, REML=FALSE)

LMM.model = lmer(PM25 ~ AOD + (1 + AOD|Day) , data=LMMexcdata,REML=FALSE)

anova(LMM.null1,LMM.model)

anova(LMM.null2,LMM.model)

#============ Output (如果需要把系数输出成文本 ,自己完善下代码) ================

#Linear Mixed effects modle

#Get the coefficient of LLM

# coefficient (fixed)

AODslopefix=as.numeric((lme4::fixef(LMM.model)[2]))

interceptfix=as.numeric(as.matrix(lme4::fixef(LMM.model)[1]))

# coefficient (radom)

AODsloperad=as.numeric(as.matrix(lme4::ranef(LMM.model)$Day[,2]))

interceptsrad=as.numeric(as.matrix(lme4::ranef(LMM.model)$Day[,1]))

 

print(AODslopefix)

print(interceptfix)

print(AODsloperad)

print(interceptsrad)

#==============================================================

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/31572.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注