0
$\begingroup$

I am trying my hands on Linear Regression using the iris dataset available on Kaggle. The columns in this dataset are:

  • Id
  • SepalLengthCm
  • SepalWidthCm
  • PetalLengthCm
  • PetalWidthCm
  • Species

The species are categorized into 3 as the following plot illustrates.

enter image description here

Now I want to predict the Species based on the input Sepal width and Sepal length.

The first thing I did was to transform the species name into numbers using the following:

transformSpecies <- function(species) {
    transformed <- -1

    if(species == 'Iris-virginica'){
      transformed <- 1
    }else if(species == 'Iris-versicolor') {
      transformed <- 2
    }else if(species == 'Iris-setosa') {
      transformed <- 3
    }else {
      print('Failed to transform species ', species)
    }
}

iris$transformedSpecies <- sapply(iris$Species, transformSpecies)

Then I constructed a linear model:

linearModel <- lm(transformedSpecies~SepalLengthCm + SepalWidthCm, iris)

The equation is same as:

y = c + M1X1 + M2X2

To find the coefficients, the above linear model was passed to the coef function.

theta <- coef(linearModel)

Then I tried to make some predictions using predict:

predict(linearModel, data.frame(SepalLengthCm = c(5,5.5,6,7.5,8.5), 
   SepalWidthCm = c(2,2.5,4.5,3.5,5)))

The results are as follows:

       1        2        3        4        5 
       1.949183 1.899571 2.807600 1.062697 1.282687 

Few questions:

  • Is it the correct way to create a linear model when there is more than one predictor variable?
  • How can I improve this linear model?
  • What is the best way to handle response variables when they are not numeric, as the case here?
$\endgroup$
1
$\begingroup$

It looks like you are fairly new to R, so first some code suggestions.

You can convert a factor to a set of integers using the as.numeric function, you don't need sapply (or other looping) for this. If you really want more control, look at the ifelse function, or indexing.

But most of the time you don't need to convert from a factor, R is very good at automatically doing the correct thing with factors.

Also, the iris dataset is one of the data sets that comes with R, you don't need to download it from elsewhere.

Next some information on linear models.

Linear models (regression) are based on the idea that the response variable is continuous and normally distributed (conditional on the model and predictor variables). Your response variable has 3 distinct values, not continuous and not really a candidate for normal.

Think about what your predictions mean, what would a prediction of 0.5 or 3.8 mean? You might interpret a 1.5 prediction as a mixture of virginica and versicolor, but then that would mean that a prediction of 2 could either be a prediction of versicolor, or a mixture of virginica and setosa, or a mixture of all 3. And what would happen if you switched the coding for virginica and versicolor? Which is 1 and which is 2 is arbitrary, but would make a big difference in the model fit.

If you really want to predict species, then a linear model really does not make sense here. The multinom function from the nnet package and the polr function in the MASS package (or similar functions in other packages) would make a lot more sense for this problem. Classification trees are another option. But you should really learn the science behind those tools before trying to use them. Statistics is more than just getting the syntax correct.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.