在R
如何绘制逻辑回归?我想在y轴上绘制因变量,在x轴上绘制独立变量。我调用了系数并得到了一个输出,所以脚本上没有错误 以下是自变量的数据(补充):在R,r,regression,data-visualization,R,Regression,Data Visualization,如何绘制逻辑回归?我想在y轴上绘制因变量,在x轴上绘制独立变量。我调用了系数并得到了一个输出,所以脚本上没有错误 以下是自变量的数据(补充): #设置供水压力范围 suppress有几种方法可以绘制逻辑回归的结果。有关R中的解释和示例,请参见和。迈克尔·J·克劳利的优秀著作《统计学:使用R的简介》中提出的一个可能的解决方案是以下代码: attach(mtcars); model <- glm(formula = am ~ hp + wt, family = binomial); print
#设置供水压力范围
suppress有几种方法可以绘制逻辑回归的结果。有关R中的解释和示例,请参见和。迈克尔·J·克劳利的优秀著作《统计学:使用R的简介》中提出的一个可能的解决方案是以下代码:
attach(mtcars);
model <- glm(formula = am ~ hp + wt, family = binomial);
print(summary(model));
model.h <- glm(formula = am ~ hp, family = binomial);
model.w <- glm(formula = am ~ wt, family = binomial);
op <- par(mfrow = c(1,2));
xv <- seq(0, 350, 1);
yv <- predict(model.h, list(hp = xv), type = "response");
hp.intervals <- cut(hp, 3);
plot(hp, am);
lines(xv, yv);
points(hp,fitted(model.h),pch=20);
am.mean.proportion <- tapply(am, hp.intervals, sum)[[2]] / table(hp.intervals)[[2]];
am.mean.proportion.sd <- sqrt(am.mean.proportion * abs(tapply(am, hp.intervals, sum)[[3]] - tapply(am, hp.intervals, sum)[[1]]) / table(hp.intervals)[[2]]);
points(median(hp), am.mean.proportion, pch = 16);
lines(c(median(hp), median(hp)), c(am.mean.proportion - am.mean.proportion.sd, am.mean.proportion + am.mean.proportion.sd));
xv <- seq(0, 6, 0.01);
yv <- predict(model.w, list(wt = xv), type = "response");
wt.intervals <- cut(wt, 3);
plot(wt, am);
lines(xv, yv);
points(wt,fitted(model.w),pch=20);
am.mean.proportion <- tapply(am, wt.intervals, sum)[[2]] / table(wt.intervals)[[2]];
am.mean.proportion.sd <- sqrt(am.mean.proportion * abs(tapply(am, wt.intervals, sum)[[3]] - tapply(am, wt.intervals, sum)[[1]]) / table(wt.intervals)[[2]]);
points(median(wt), am.mean.proportion, pch = 16);
lines(c(median(wt), median(wt)), c(am.mean.proportion - am.mean.proportion.sd, am.mean.proportion + am.mean.proportion.sd));
detach(mtcars);
par(op);
附加(mtcars);
模型你能在这里封装代码使这个答案完整吗?
#Create logistic regression
z=1+2*NozHosUn+3*SupPres+4*PlaceSet+5*Hrs4+6*WatTemp
z <- (z-mean(z))/sd(z)
pr = 1/(1+exp(-z))
y <- rbinom(3000,1,pr)
DishWa=data.frame(y=y, NozHosUn=NozHosUn,SupPres=SupPres,
PlaceSet=PlaceSet,Hrs4=Hrs4,WatTemp=WatTemp)
glm(y~NozHosUn+SupPres+PlaceSet+Hrs4+WatTemp, data=DishWa,
family=binomial)
attach(mtcars);
model <- glm(formula = am ~ hp + wt, family = binomial);
print(summary(model));
model.h <- glm(formula = am ~ hp, family = binomial);
model.w <- glm(formula = am ~ wt, family = binomial);
op <- par(mfrow = c(1,2));
xv <- seq(0, 350, 1);
yv <- predict(model.h, list(hp = xv), type = "response");
hp.intervals <- cut(hp, 3);
plot(hp, am);
lines(xv, yv);
points(hp,fitted(model.h),pch=20);
am.mean.proportion <- tapply(am, hp.intervals, sum)[[2]] / table(hp.intervals)[[2]];
am.mean.proportion.sd <- sqrt(am.mean.proportion * abs(tapply(am, hp.intervals, sum)[[3]] - tapply(am, hp.intervals, sum)[[1]]) / table(hp.intervals)[[2]]);
points(median(hp), am.mean.proportion, pch = 16);
lines(c(median(hp), median(hp)), c(am.mean.proportion - am.mean.proportion.sd, am.mean.proportion + am.mean.proportion.sd));
xv <- seq(0, 6, 0.01);
yv <- predict(model.w, list(wt = xv), type = "response");
wt.intervals <- cut(wt, 3);
plot(wt, am);
lines(xv, yv);
points(wt,fitted(model.w),pch=20);
am.mean.proportion <- tapply(am, wt.intervals, sum)[[2]] / table(wt.intervals)[[2]];
am.mean.proportion.sd <- sqrt(am.mean.proportion * abs(tapply(am, wt.intervals, sum)[[3]] - tapply(am, wt.intervals, sum)[[1]]) / table(wt.intervals)[[2]]);
points(median(wt), am.mean.proportion, pch = 16);
lines(c(median(wt), median(wt)), c(am.mean.proportion - am.mean.proportion.sd, am.mean.proportion + am.mean.proportion.sd));
detach(mtcars);
par(op);