R语言分析协变量之间的非线性关系
时间:2022-07-22
本文章向大家介绍R语言分析协变量之间的非线性关系,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
原文链接:http://tecdat.cn/?p=6366
最近我被问到我的 - [R和Stata的软件包是否能够适应协变量之间的非线性关系。答案是肯定的,在这篇文章中,我将说明如何做到这一点。
为了说明,我们将模拟具有两个协变量X1和X2以及连续结果ý的非常大的数据集。
set.seed(123)
n < - 10000
x1 < - rnorm(n)
x2 < - x1 ^ 2 + rnorm(n)
y < - x1 + x2 + rnorm(n)
[(runif(n)<expit(y))] < - NA
mydata < - data.frame(x1, X2,Y)
因此,模型的真实系数是0(截距)。注意,实体模型中没有非线性,但x2对x1的依赖性存在非线性。
imps1 < - (mydata,smtype =“lm” ,
numit = 50,method = c(“”,“norm”,“”))
impobj < - imputationList(imps1 $ impDatasets)
输出:
[1] "Outcome variable(s): y"
[1] "Passive variables: "
[1] "Partially obs. variables: x2"
[1] "Fully obs. substantive model variables: x1"
[1] "Imputation 1"
[1] "Imputing: x2 using x1,x1sq plus outcome"
[1] "Imputation 2"
[1] "Imputation 3"
[1] "Imputation 4"
[1] "Imputation 5"
Warning message:
In smcfcs.core(originaldata, smtype, smformula, method, predictorMatrix, :
Rejection sampling failed 503 times (across all variables, iterations, and imputations). You may want to increase the rejection sampling limit.
Multiple imputation results:
with(impobj, lm(y ~ x1 + x2))
MIcombine.default(models)
results se (lower upper) missInfo
(Intercept) -0.0274234 0.015746687 -0.06054163 0.005694823 53 %
x1 1.0075646 0.018740270 0.96407720 1.051052088 77 %
x2 1.0026004 0.008043873 0.98549090 1.019709850 56 %
我们看到x1的截距和系数的估计有明显的偏差。假设x2遵循以x1为条件的线性回归模型,smcfcs正在估算x2中的缺失值,条件均值在x1中是线性的。这样做意味着X2平方会在X2的插补模型中自动调整:
mydata $ x1sq < - mydata $ x1 ^ 2
imps2 < - (mydata,smtype =“lm”,smformula =“y~x1 + x2 + x1sq”,
numit = 50,method = c(“”,“norm”, “”,“”))
impobj < - imputationList(imps2 $ impDatasets)
输出:
[1] "Outcome variable(s): y"
[1] "Passive variables: x1sq"
[1] "Partially obs. variables: x1,x2"
[1] "Fully obs. substantive model variables: "
[1] "Imputation 1"
[1] "Imputing: x1 using x2 plus outcome"
[1] "Imputing: x2 using x1,x1sq plus outcome"
[1] "Imputation 2"
[1] "Imputation 3"
[1] "Imputation 4"
[1] "Imputation 5"
Warning message:
In smcfcs.core(originaldata, smtype, smformula, method, predictorMatrix, :
Rejection sampling failed 17260 times (across all variables, iterations, and imputations). You may want to increase the rejection sampling limit.
Multiple imputation results:
with(impobj, lm(y ~ x1 + x2))
MIcombine.default(models)
results se (lower upper) missInfo
(Intercept) 0.2687343 0.04002737 0.1694782 0.3679903 88 %
x1 1.0276229 0.03432337 0.9436348 1.1116109 86 %
x2 1.0742299 0.01635284 1.0385746 1.1098852 64 %
我们现在估计与数据生成机制中使用的真实值非常接近。
需要注意的一点是,我们已经修改了假设为x2 | X1的模型,但我们还将实体模型(至少是用作插补过程的一部分的模型)修改为包含x1sq的模型。
predictorMatrix < - array(0,dim = c(4,4))
predictorMatrix [2,c(1,4)] < - 1
imps3 < - (mydata,smtype =“lm”,smformula =“y~x1 + x2“,numit = 50,
predictorMatrix = predictorMatrix )
impobj < - imputationList(imps3 $ impDatasets)
models < - with(impobj,lm(y~x1) + x2))
输出:
[1] "Outcome variable(s): y"
[1] "Passive variables: "
[1] "Partially obs. variables: x2"
[1] "Fully obs. substantive model variables: x1"
[1] "Imputation 1"
[1] "Imputing: x2 using x1,x1sq plus outcome"
[1] "Imputation 2"
[1] "Imputation 3"
[1] "Imputation 4"
[1] "Imputation 5"
Warning message:
In smcfcs.core(originaldata, smtype, smformula, method, predictorMatrix, :
Rejection sampling failed 503 times (across all variables, iterations, and imputations). You may want to increase the rejection sampling limit.
Multiple imputation results:
with(impobj, lm(y ~ x1 + x2))
MIcombine.default(models)
results se (lower upper) missInfo
(Intercept) -0.0274234 0.015746687 -0.06054163 0.005694823 53 %
x1 1.0075646 0.018740270 0.96407720 1.051052088 77 %
x2 1.0026004 0.008043873 0.98549090 1.019709850 56 %
这里完全观察到x1。如果x1也有一些缺失值怎么办?然后我们需要告诉smcfcs如何估算x1,然后被动地估算x1sq变量。鉴于我们对真实数据生成模型的了解,我们应该如何归认于x1?然而,我们将继续,要求smcfcs使用规范方法来估算X1:
mydata$x1[runif(n)<0.25] <- NA
mydata$x1sq <- mydata$x1^2
predictorMatrix[1,2] <- 1
imps4 <- (mydata, smtype="lm", smformula = "y~x1+x2", numit=50,
predictorMatrix=predictorMatrix, =c("norm","norm","","x1^2"))
impobj <- (imps4$impDatasets)
models <- with(impobj, lm(y~x1+x2))
summary(MIcombine(models))
输出:
[1] "Outcome variable(s): y"
[1] "Passive variables: x1sq"
[1] "Partially obs. variables: x1,x2"
[1] "Fully obs. substantive model variables: "
[1] "Imputation 1"
[1] "Imputing: x1 using x2 plus outcome"
[1] "Imputing: x2 using x1,x1sq plus outcome"
[1] "Imputation 2"
[1] "Imputation 3"
[1] "Imputation 4"
[1] "Imputation 5"
Warning message:
In smcfcs.core(originaldata, smtype, smformula, method, predictorMatrix, :
Rejection sampling failed 17260 times (across all variables, iterations, and imputations). You may want to increase the rejection sampling limit.
Multiple imputation results:
with(impobj, lm(y ~ x1 + x2))
MIcombine.default(models)
results se (lower upper) missInfo
(Intercept) 0.2687343 0.04002737 0.1694782 0.3679903 88 %
x1 1.0276229 0.03432337 0.9436348 1.1116109 86 %
x2 1.0742299 0.01635284 1.0385746 1.1098852 64 %
这个例子也说明了smcfcs的一个理论问题 - 虽然它从一个与指定的实体或结果模型兼容的插补模型中推算每个协变量,但这并不意味着这些插补模型中的每一个都是相互兼容的。具体而言,用于分配其他协变量的模型可能不兼容。
更有效的方法是为数据指定单个联合模型,并在其隐含的条件分布下进行估算。例如,这可以使用JAGS来实现。
- 低级程序员和高级程序员的区别
- Silverlight学习(二)
- Silverlight学习(三)
- ArcGIS for Android学习(一)
- Arcgis for Silverlight学习(一)
- ImageButton与Button
- RadioButton、CheckBox与ToggleButton
- Silverlight学习(五)图形标绘
- radio与checkbox
- JavaScript之cookie
- Silverlight学习(四) domainservice动态多条件查询
- Android学习Tabhost、gallery、listview、imageswitcher
- ProgressBar、RatingBar和Spinner控件
- TimePicker控件、帧动画、补间动画
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 案例:记录一则强制开库遭遇ORA-16433的处理过程
- mybatis升级为mybatis-plus踩到的坑
- Treepath
- linux 远程ssh免密登录
- npm 安装 electron taobao镜像 404错误 自用 实践笔记
- Asp.net Core 使用Jenkins + Dockor 实现持续集成、自动化部署(二):部署
- 队列的一种实现:循环队列
- StackExchange.Redis .net core Timeout performing 超时问题
- G1 垃圾回收器简单调优
- Docker安装官方Redis镜像并启用密码认证 实践笔记
- Asp.net Core 使用Jenkins + Dockor 实现持续集成、自动化部署(四):发布与回滚
- linux centos 安装Jenkins(非docker方式)
- mysql5.7 derived_merge=on 弄丢了我的 order by!
- 用nrm轻松管理npm镜像 自用 实践笔记
- 模拟战役(DFS||并查集解法)