Warning message: 'newdata'必需有1行 但变量里有45行

data<-read.xlsx('msaD.xlsx','d5.1')    #导入数据
g<-glm(data$y~data$x1+data$x2+data$x3,family = binomial) #广义线性模型
summary(g)     
logis.step<-step(g,direction = 'both')    #逐步回归法选择变量
pre<-predict(logis.step,data.frame(x1=1))   #预测分类变量x1的值为1时因变量的值
p2<-exp(pre)/(1+exp(pre))
p2  

此时会出现警告以及p2概率重复多遍,如

Warning message:
'newdata'必需有1行 但变量里有45行 
> p2<-exp(pre)/(1+exp(pre))
> p2
   1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19 
0.32 0.32 0.32 0.32 0.32 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0.32 0.32 0.32 0.32 
  20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38 
0.32 0.32 0.32 0.32 0.32 0.32 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0.65 0.32 0.32 0.32 
  39   40   41   42   43   44   45 
0.32 0.32 0.32 0.32 0.32 0.32 0.32 

原因是预测时输入的变量名x1不够精确,虽说实际导入的数据里的变量名就是x1,但跟模型里描述的不同,函数glm()由于没绑定数据所以描述x1的是data$x1,因此可以选择以下三种方式之一:

1.使用attach(data)先绑定数据,后用变量名x1,x2,x3构造模型并进行预测,不过使用attach之后想解除绑定并绑定其他数据容易出错,所以不是很推荐。

 

 

2.

data<-read.xlsx('msaD.xlsx','d5.1')
gl<-glm(y~x1+x2+x3,family = binomial,data)  #指定数据,可直接用变量名访问
gl
data.step<-step(gl,direction = 'both')
pre<-predict(data.step,data.frame(x1=1))
p<-exp(pre)/(1+exp(pre))
p

 3.

data<-read.xlsx('msaD.xlsx','d5.1')
x1<-data$x1     #先将变量名换成与预测时使用的一致
g<-glm(data$y~x1+data$x2+data$x3,family = binomial)    #模型输入简化的x1
summary(g)
logis.step<-step(g,direction = 'both')
pre<-predict(logis.step,data.frame(x1=1))   #直接使用x1预测
p2<-exp(pre)/(1+exp(pre))
p2
正常控制台输出
> pre<-predict(logis.step,data.frame(x1=1))
> p2<-exp(pre)/(1+exp(pre))
> p2
   1 
0.32 

执行命令应注意步骤顺序以及多次执行导致变量名间冲突等情况,会使命令仍不尽人意。

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace 任意数计算 { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(16, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(88, 24); this.label1.TabIndex = 0; this.label1.Text = "请输入数据:"; // // label2 // this.label2.Location = new System.Drawing.Point(0, 104); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(224, 40); this.label2.TabIndex = 1; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(136, 24); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(120, 21); this.textBox1.TabIndex = 2; this.textBox1.Text = ""; // // button1 // this.button1.Location = new System.Drawing.Point(56, 64); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(88, 32); this.button1.TabIndex = 3; this.button1.Text = "计算"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { int i; i=int.Parse(textBox1.Text); int m=0; int num=1; int[] js=new int[i+2]; for(int t=0;t<i;t++) js[t]=1; int j=i; for(;;) { if(js[j-1]>js[j-2]+1) { int t=js[j-1]-1; js[j-2]++;j--; for(;;) { if(t>=js[j-1]*2){j++;js[j-1]=js[j-2];t=t-js[j-1];} else { j++;js[j-1]=t;break;} } } else { js[j-2]+=js[j-1]; j--; } m++; if(js[0]==i)break; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值