全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
2836 2
2013-11-24
现在有8个变量,在Logistic回归中剔除了4个,剩余4个已经建立Logistic模型。想把剔除的4个变量加回来,作为非线性部分,进行半参数回归,求R实现方法
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-11-24 14:55:36
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-2-13 23:29:14
这是用mgcv包配合写的,你看下能不能用
  
mfxboot <- function(modform,dist,data,boot=1000,digits=3){ #dist is the distribution choice of logit or probit
      require(mgcv)

x <- gam(modform, family=binomial(link=dist),method="GCV.Cp",data)
      # get marginal effects

pdf <- ifelse(dist=="probit",               
mean(dnorm(predict(x, type = "link")))            
mean(dlogis(predict(x, type = "link")))
marginal.effects <- pdf*coef(x)


bootvals <- matrix(rep(NA,boot*length(coef(x))), nrow=boot)  
set.seed(1111)  
for(i in 1:boot){   
samp1 <- data[sample(1:dim(data)[1],replace=T,dim(data)[1]),]   
x1 <- gam(modform, family=binomial(link=dist),method="GCV.Cp",samp1)   
pdf1 <- ifelse(dist=="probit",                  
mean(dnorm(predict(x1, type = "link"))),                  
mean(dlogis(predict(x1, type = "link"))))      
bootvals[i,] <- pdf1*coef(x1)     
}

res <- cbind(marginal.effects,apply(bootvals,2,sd),marginal.effects/apply(bootvals,2,sd))     
if(names(x$coefficients[1])=="(Intercept)"){        
res1 <- res[2:nrow(res),]   
res2 <- matrix(as.numeric(sprintf(paste("%.",paste(digits,"f",sep=""),sep=""),res1)),nrow=dim(res1)[1])         
rownames(res2) <- rownames(res1)        
} else {   
res2 <- matrix(as.numeric(sprintf(paste("%.",paste(digits,"f",sep=""),sep="")),nrow=dim(res)[1]))      
rownames(res2) <- rownames(res)   
}     
colnames(res2) <- c("marginal.effect","standard.error","z.ratio")
      return(res2)
}

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群