分段分段线性拟合的约束系数

时间:2017-01-10 22:01:56

标签: r lm

我正在尝试将数据拟合到分段线性模型中。分段包非常有效,除了我找不到将约束应用于系数的方法。我需要将线性段的斜率约束到一个值范围(例如,对于段1,0-0.1,对于段2,> 0.5)。这是一个没有约束的例子。感谢。

library(segmented)

#generate data for fit
c <- 1 
m <- 0.47 #slope of 2nd line
d <- 4
n<- 30
sd <- 0.2
b <- c-m*d
x<- runif(n,0,10)
y<- ifelse(x<=d,c+rnorm(n,0,sd),m*x+b+rnorm(n,0,sd)) #piecewise data for fit
plot(x,y)

lin.mod <- lm(y~x)
segmented.mod <- segmented(lin.mod, seg.Z = ~x, psi=6)
summary(segmented.mod)
plot(segmented.mod, add=T)

1 个答案:

答案 0 :(得分:0)

这可能不是优雅的方法,但我使用constrOptim管理了一个解决方案。

$file= Input::file('image');
        if(!empty($file)){
        $filename  = $file->getClientOriginalExtension();
         // please identify you upload location
         $request->file('image')->move( base_path() . '/public/uploads/', $filename );
     $insertdata = array('title'=>$filename);
     DB::table('table')->insert($insertdata );

}