0

I've been trying to make a univariate linear regression with two columns of my data imported from an csv file. However, the program does not seem to recognise the name of the columns whether I name them after their headers or the column's name in RStudio (V1, V2 etc.).

patient_data1 = read.csv("C:\\PATHWAY_PLACEHOLDER, header = TRUE")

patient_data2 = data.frame(patient_data1)



attach(patient_data)

names(patient_data)

class(HR)


## plot a x/y scatter plot with the data
plot(HR, RAZscor, main = "Scatterplot")

cor(HR, RAZscor)

## create a "linear model" = the regression
patient_regression <- lm(HR ~ RAZscor, data=patient_data)
## generate a summary of the regression
summary(patient_regression)

## add the regression line to x/y scatter plot
abline(patient_regression, col="blue")

When running the program, the error message "object 'HR' not found" shows up. I'm new to R and have been trying different things, but can't seem to figure out what I'm doing wrong.

I would expect the two columns named "HR" and "RAZscor" in the file to be used as x- and y-coordinates for data points in a scatter plot, in which a linear regression would be formed. I've tried using different techniques to save the columns in objects, and I've tried e.g. calling the objects with the method "patient_data2$HR" when plotting it, but then the object "patient_data2" cannot be found either.

joeke
  • 21
  • 2
  • Maybe patient_data1! **1** – TarJae Feb 07 '23 at 04:56
  • I've tried calling patient_data1 instead, but the problem remains. – joeke Feb 07 '23 at 08:19
  • First, you confuse yourself by using `patient_data`, `patient_data1`, `patient_data2`, and probably used the wrong one in `lm`. Second, you use `attach` which is **very** dangerous (see this [thread](https://stackoverflow.com/q/10067680/6574038)) and I strongly advise you not to use it! If you would modify a column in the attached data, then use it together with the unmodified data frame you really get a mess (in the script was well as in your head)! Delete that line, and do e.g. `patient_data$HR.` Good luck! – jay.sf Feb 07 '23 at 08:19

0 Answers0