R编程错误:变数的长度不一样

进行分段线性回归分析时,输入如下公式:

myLmFastGrowth = lm(njagr_weight_data$Weight ~ njagr_weight_data$Week[njagr_weight_data$Week <= 2], data = njagr_weight_data)

执行上述公式,console显示“变数的长度不一样”错误。仔细分析发现,作为因变量的njagr_weight_data$Weight没有经过条件筛选,数据长度确实比自变量njagr_weight_data$Week经过njagr_weight_data$Week <= 2条件筛选后的长度长,所以,对因变量也进行与自变量同样的条件筛选,将公式修改如下:

myLmFastGrowth = lm(njagr_weight_data$Weight[njagr_weight_data$Week <= 2] ~ njagr_weight_data$Week[njagr_weight_data$Week <= 2], data = njagr_weight_data)

你可能感兴趣的:(R,R,变数的长度)