質問

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