1

I have the following dataset named fish_data

>     structure(list(Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Bream",  "Parkki", "Perch", "Pike", "Roach", "Smelt", "Whitefish"), class = "factor"), 
>     WeightGRAM = c(242, 290, 340, 363, 430, 450), VertLengthCM = c(23.2, 24, 23.9, 26.3, 26.5, 26.8)
>     DiagLengthCM = c(25.4, 26.3, 26.5, 29, 29, 29.7), 
>     CrossLengthCM = c(30, 31.2, 31.1, 33.5, 34, 34.7), 
>     HeightCM = c(11.52, 12.48, 12.3778, 12.73, 12.444, 13.6024), 
>     WidthCM = c(4.02, 4.3056, 4.6961, 4.4555, 5.134, 4.9274)), 
>     row.names = c(NA, -6L), class = c("tbl_df", "tbl",  "data.frame"), na.action = structure(c(`41` = 41L), class = "omit"))

It look something like this:

enter image description here

How can i Build a linear regression model named m1 with WeightGRAM as a function of Species and all the measurement variables i.e. VertLengthCM, DiaLengthCM, CrossLengthCM, HeightCM, WidthCM?

i have the linear regression code as below:

m1 <- lm(WeightGRAM~.,data = fish_data )
summary(m1)

But i want to exclude the "species" as it is a factor

2 Answers2

2

You can try this:

#Index
index <- which(names(fish_data)=='Species')
#Model
m1 <- lm(WeightGRAM~.,data = fish_data[,-index] )

Call:
lm(formula = WeightGRAM ~ ., data = fish_data[, -index])

Coefficients:
  (Intercept)   VertLengthCM   DiagLengthCM  CrossLengthCM       HeightCM        WidthCM  
      -827.56        -124.85          70.08          72.14         -23.41          72.52  
Duck
  • 39,058
  • 13
  • 42
  • 84
0

You can check the if the column is numeric or not using is.numeric which returns a logical value. You can use it to subset fish_data.

cols <- sapply(fish_data, is.numeric)
m1 <- lm(WeightGRAM~.,data = fish_data[, cols])
m1

#Call:
#lm(formula = WeightGRAM ~ ., data = fish_data[, cols])

#Coefficients:
#  (Intercept) VertLengthCM DiagLengthCM  CrossLengthCM     HeightCM     WidthCM  
#       -827.6       -124.8         70.1           72.1        -23.4        72.5  
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213