As always a more colourful version of this post is available on rpubs.

Even if LM are very simple models at the basis of many more complex ones, LM still have some assumptions that if not met would render any interpretation from the models plainly wrong. In my field of research most people were taught about checking ANOVA assumptions using tests like Levene & co. This is however not the best way to check if my model meet its assumptions as p-values depend on the sample size, with small sample size we will almost never reject the null hypothesis while with big sample even small deviation will lead to significant p-values (discussion). As ANOVA and linear models are two different ways to look at the same model (explanation) we can check ANOVA assumptions using graphical check from a linear model. In R this is easily done using plot(model), but people often ask me what amount of deviation makes me reject a model. One easy way to see if the model checking graphs are off the charts is to simulate data from the model, fit the model to these newly simulated data and compare the graphical checks from the simulated data with the real data. If you cannot differentiate between the simulated and the real data then your model is fine, if you can then try again!

Below is a little function that implement this idea:

lm.test<-function(m  require(plyr)
#the model frame
dat<-model.frame(m)
#the model matrix
f<-formula(m)
modmat<-model.matrix(f,dat)
#the standard deviation of the residuals
sd.resid<-sd(resid(m #sample size
n<-dim(dat)[1]
#get the right-hand side of the formula
#rhs<-all.vars(update(f, 0~.))
#simulate 8 response vectors from model
ys<-lapply(1:8,function(x) rnorm(n,modmat%*%coef(m),sd.resid))
#refit the models
ms<-llply(ys,function(y) lm(y~modmat[,-1]))
#put the residuals and fitted values in a list
df<-llply(ms,function(x) data.frame(Fitted=fitted(x),Resid=resid(x)))
#select a random number from 2 to 8
rnd<-sample(2:8,1)
#put the original data into the list
df<-c(df[1:(rnd-1)],list(data.frame(Fitted=fitted(m),Resid=resid(m))),df[rnd:8]) #plot
par(mfrow=c(3,3))
l_ply(df,function(x){
plot(Resid~Fitted,x,xlab="Fitted",ylab="Residuals")
abline(h=0,lwd=2,lty=2)
}) l_ply(df,function(x){
qqnorm(x$Resid)
qqline(x$Resid)
}) out<-list(Position=rnd)
return(out)
}

This function print the two basic plots: one looking at the spread of the residuals around the fitted values, the other one look at the normality of the residuals. The function return the position of the real model in the 3×3 window, counting from left to right and from top to bottom (ie position 1 is upper left graph).

Let’s try the function:

#a simulated data frame of independent variables
dat<-data.frame(Temp=runif(100,0,20),Treatment=gl(n = 5,k = 20))
contrasts(dat$Treatment)<-"contr.sum"
#the model matrix
modmat<-model.matrix(~Temp*Treatment,data=dat)
#the coefficient
coeff<-rnorm(10,0,4)
#simulate response data
dat$Biomass<-rnorm(100,modmat%*%coeff,1)
#the model
m<-lm(Biomass~Temp*Treatment,dat)
#model check
chk<-lm.test(m)

Can you find which one is the real one? I could not, here is the answer:

chk
$Position
[1] 4

Happy and safe modelling!

转自:https://biologyforfun.wordpress.com/2015/03/25/a-function-to-help-graphical-model-checks-of-lm-and-anova/

A function to help graphical model checks of lm and ANOVA(转)的更多相关文章

  1. PGM:概率图模型Graphical Model

    http://blog.csdn.net/pipisorry/article/details/51461878 概率图模型Graphical Models简介 完全通过代数计算来对更加复杂的模型进行建 ...

  2. 概率图模型(PGM,Probabilistic Graphical Model)

    PGM是现代信号处理(尤其是机器学习)的重要内容. PGM通过图的方式,将多个随机变量之前的关系通过简洁的方式表现出来.因此PGM包括图论和概率论的相关内容. PGM理论研究并解决三个问题: 1)表示 ...

  3. [zz] 混合高斯模型 Gaussian Mixture Model

    聚类(1)——混合高斯模型 Gaussian Mixture Model http://blog.csdn.net/jwh_bupt/article/details/7663885 聚类系列: 聚类( ...

  4. 构建自己的PHP框架--实现Model类(1)

    在之前的博客中,我们定义了ORM的接口,以及决定了使用PDO去实现.最后我们提到会有一个Model类实现ModelInterface接口. 现在我们来实现这个接口,如下: <?php names ...

  5. Implementation Model Editor of AVEVA in OpenSceneGraph

    Implementation Model Editor of AVEVA in OpenSceneGraph eryar@163.com 摘要Abstract:本文主要对工厂和海工设计软件AVEVA的 ...

  6. 【再探backbone 01】模型-Model

    前言 点保存时候不注意发出来了,有需要的朋友将就看吧,还在更新...... 几个月前学习了一下backbone,这段时间也用了下,感觉之前对backbone的学习很是基础,前几天有个园友问我如何将路由 ...

  7. PHP MVC 中的MODEL层

    Model层,就是MVC模式中的数据处理层,用来进行数据和商业逻辑的装封 三.实现你的Mode层 Model层,就是MVC模式中的数据处理层,用来进行数据和商业逻辑的装封,进行他的设计的时候设计到三个 ...

  8. hdwiki中model模块的应用

    control中调用model原则是这样的,如果你的这个model在本control中大部分方法中都要用到,那么,就写在构造函数里面.例如,名字为doc的control的构造函数如下: functio ...

  9. [Backbone.js]如何处理Model里面嵌入的Collection?

    写了近半个月的backbone.js代码,从一开始的todo到现在做仿微信的网页聊天,其中最大的困惑就在于如何处理比较复杂的Model,其内嵌了一个或者多个Collections. 假设我们有一个Pe ...

随机推荐

  1. filter滤镜的使用

    刚开始学css,开始遇到filter不懂什么意思后来到网上查了,觉得解释的很全面,就把它抠下来,以便自己经常来看看. CSS滤镜的使用方法:filter:filtername(parameters) ...

  2. APP被苹果App Store拒绝的79个原因【转】

    作为iOS开发者,估计有很多都遇到过APP提交到App Store被拒,然后这些被拒的原因多种多样,今天dApps收集了常见的被拒的原因,以便更多开发者了解. APP被苹果APPStore拒绝的各种原 ...

  3. 【PAT_Basic日记】1005. 继续(3n+1)猜想

    #include <stdio.h> #include <stdlib.h> /** 逻辑上的清晰和代码上的清晰要合二为一 (1)首先在逻辑上一定要清晰每一步需要干什么, (2 ...

  4. 【Tomcat源码学习】-5.请求处理

    前四章节,主要对Tomcat启动过程中,容器加载.应用加载.连接器初始化进行了相关的原理和代码流程进行了学习.接下来开始进行接受网络请求后的相关处理学习.   一.整体流程      基于上一节图示进 ...

  5. 2017年4月 TIOBE 编程语言排名

    2017年4月 TIOBE 编程语言排名 Hack是Facebook 在三年推出的PHP方言,在2017年4月首次进入TIOBE编程语言排行榜前50位. Hack原是Facebook的内部项目,与20 ...

  6. javaWeb项目(SSH框架+AJAX+百度地图API+Oracle数据库+MyEclipse+Tomcat)之一 基础Struts框架搭建篇

    即将开始着手写这个项目,所以希望通过这篇博客来记录自己学习的过程 今天开学第一天,就上了软件工程实践课,自己也开始着手做这个大作业了.首先我的项目名称叫做智能班车管理系统. 项目的概况: 该软件产品是 ...

  7. git 设置不需要输入密码, 去除 fetch / pull 代码每次都需要输入密码的烦恼

    https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速 设置记住密码(默认15分钟): git config --global credenti ...

  8. Windows7系统的封装

    系统装到虚拟机优化完成以后,可以装上自己想要装的软件,也可以不装做个纯净版的.今天用ES封装,首先打开一般我们只点这四项就够了,如下图 写上自己想写的,也可以不写系统默认,直接点“下一步”关闭设备管理 ...

  9. [KISSY5系列]淘宝全终端框架 KISSY 5--从零开始使用

    KISSY 是淘宝一个开源的 JavaScript 库,包含的组件有:日历.图片放大镜.卡片切换.弹出窗口.输入建议等 一.简介 KISSY 是一款跨终端.模块化.高性能.使用简单的 JavaScri ...

  10. 为RecyclerView的item之间设置相同的间距

    项目中经常碰到列表当中的每一项之间需要设置间距的问题,我们可以通过给列表中的每一项设置margin值来实现,例如纵向的间距可以给每一项设置right_margin,这种方法下,整个列表的最左边会紧贴屏 ...