java predict 函数,R中的Predict()函数 . 如何使用它来预测因变量

我有一个关于如何使用函数predict()的问题 .

我有一个包含n行和10列的数据集 . 第一列是因变量,其他变量是独立的 . 我在第一个变量上有50%的缺失数据,即x1,其他变量被完全观察到 . 我想通过使用以下模型中的对应案例和回归系数来预测x1(缺失部分):

lm(new_A[,1]~new_A[,2]+new_A[,3]+new_A[,4]+new_A[,5]+new_A[,6]+new_A[,7]+new_A[,8]+new_A[,9]+new_A[,10]).

这是我的代码:

set.seed(40)

n=10000 # number of observation

m=10 # number of variables

mis=50 # percentage of missing data on the dependent variable

# I randomly assign 50% of missing data on the first variable (the dependent one in my future model)

for (i in 1:m){

X[,i]=runif(n,0,1)

}

X=data.frame(X)

# I randomly assign 50% of missing data on the first variable (the dependent one in my future model)

aa=runif(n,0,1)

X_MCAR[which(aa<=sort(aa)[mis*(n/100)]),1]=NA

# First, I create 2 datasets. One for the group A (x1,obs, x2|x1,obs, x3|x1,obs, ..., xm|x1,obs) --> new_A

# and one for group B (x1,mis, x2|x1,mis, x3|x1,mis, ..., xm|x1,mis) --> new_B

new_A=data.frame(X_MCAR[is.na(X_MCAR[,1])==FALSE,])

new_B=data.frame(X_MCAR[is.na(X_MCAR[,1])==TRUE,])

# Second, I stock the result of the regression on the group A in reg_A.

reg_A=lm(new_A[,1]~new_A[,2]+new_A[,3]+new_A[,4]+new_A[,5]+new_A[,6]+new_A[,7]+new_A[,8]+new_A[,9]+new_A[,10])

# Third, I predict x1,obs and x1,mis by using the regression coefficient from A.

x1_obs_hat=predict(reg_A, new_A)

x1_mis_hat=predict(reg_A, new_B)

它们应该是不同的,但实际上它们完全相同 . 谁能帮助我并说出代码中出了什么问题?因为x1_obs_hat和x1_mis_hat应该不同,因为我对B组使用不同的观察结果 .

谢谢 :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值