0

I have a data frame with about 800 points in it. The table looks roughly like this:

V4 V92
.5 .02
.25 .12
.5 1.02
.45 -.02
.5 .32
1.5 .42

This goes on for about 850 rows.

I ran a linear regression for this using the code:

lmMRE <- lm(data.frame$V92~data.frame$V4)

For some reason, when I run this code, my regression has 157 coefficients.

Why might this be? How do I change this so I only have 1?

Thanks!

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
EamonS
  • 1
  • 6
    It sounds like your values are characters/factors rather than numeric columns. We can't really tell based on what you have provided. Make sure to share data in [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) like `dput()`. You can also try `lm(data.frame$V92~as.numeric(as.character(data.frame$V4)))` but it would be better to properly import your data in the first place. – MrFlick Jul 30 '21 at 22:57
  • 1
    Try [to convert factor to numeric](https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-integer-numeric-without-loss-of-information). – Rui Barradas Jul 31 '21 at 05:04

1 Answers1

0

There is clearly something wrong with your input data. Simulating your data gives the expected one coefficient. Check that both your dependent and independent data are numeric.

See code below.

library(tidyverse)

#create fake data
my_data <- data.frame("V4" = 1:800 + rnorm(800, 0, 2),
                      "V92" = seq(from = 1, to = 100, length.out = 800) ) 

#run regression
lmMRE <- lm(my_data$V92~my_data$V4)

#one coefficient
lmMRE

my_data %>%
  ggplot(aes(x = V4, y = V92)) +
  geom_point()+
  geom_smooth(method=lm) +
  labs(title = "Sample regression",
       subtitle = paste0("Number of coefficients: ",
                         length(lmMRE$coefficients) - 1))

Sample Regression