문제

I am writing a function which plots a linear regression in a subset of my variable.

FitWeibull <- function(data, xinf, xsup){
  xlim <- c(xinf, xsup)
  sub.data <- data
  my.lm <- lm(Y~X, data = sub.data, subset = xlim)
  return(my.lm)
 }

Will this function let me draw my regression between xinf and xsup and keep the other points on the graph?

도움이 되었습니까?

해결책

You need to specify on what variable you are subsetting. Easiest thing is replacing your sub.data <- data line with: sub.data <- data[(X>xinf & X<xsup),], so that you just have

FitWeibull <- function(data, xinf, xsup){
  sub.data <- data[(data$X > xinf & data$X < xsup),]
  my.lm <- lm(Y~X, data = sub.data)
  return(my.lm)
}

You can then do whatever plotting/summarizing you want with the returned object.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top