0

How to I rewrite this code library(ISLR) data(Auto) fit <- lm(mpg ~ horsepower, data = Auto) summary(fit) with a python code

I tried typing the above code in Jupyter notebook but instead, I got error messages

  • I note at the end of your post you mention, "I tried typing the above code in Jupyter notebook". Are you aware you can use the R-based kernel in a Jupyter notebook? Jupyter is agnostic to language but you have to have that language installed and signal to Jupyter which to use. If your only reason to post this question was to be able to work in Jupyter notebook then that would be a better path to pursue than converting this from R to Python. If you click on the 'launch binder' badge next 'Jupyter+R' [here](https://github.com/binder-examples/r), you can see an example. You code won't work ... – Wayne Feb 11 '23 at 18:46
  • there without customization though because I suspect it doesn't have the library you want. If you are lucky you may be able to install it in that example session and try it. However, it is often the case with R packages with complex dependencies that you have to build the image backing such sessions with the dependencies so they'll work when the session is served. Anyways, it is just meant to illustrate you can probable set up where you were working with Jupyter to run R code. Also there is `pyper`, see [here](https://stackoverflow.com/a/54459548/8508004). – Wayne Feb 11 '23 at 18:58
  • Also, in general you should be specific in your post title. You are looking to convert a very particular thing. Your current post title doesn't get at that and so it won't be obvious to people who may be able to guide you better or others following in your steps. Remember someone following in your steps may be you six months from now and so write the title to help yourself. – Wayne Feb 11 '23 at 19:06

1 Answers1

-1

If you do wish to switch to using Python, see this answer to 'What is the equivalent of R's lm function for fitting simple linear regressions in python?'. That also includes the equivalent to the line summary(fit).

Also related is here or here or here or R vs Python: Linear Regression.

Wayne
  • 6,607
  • 8
  • 36
  • 93