Tags
 IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6

# Regression Line plot

By : BlackfyreBastard
Date : July 30 2020, 11:00 AM
I hope this helps . The problem is you are doing multiple linear regression. You should expect a straight line if Engine size is the only factor affecting CO2 emissions. But there are other factors too. If you have 2 independent variables, you will get a plane in 3D. If you have n variables, you should expect a linear shape in the n-dimensional metric space.
code :

Share :

## ggplot2: Logistic Regression - plot probabilities and regression line

By : MZ4Frost
Date : March 29 2020, 07:55 AM
I wish this help you I have a data.frame containing a continuous predictor and a dichotomous response variable. , There are basically three solutions:
Merging the data.frames
code :
``````mydf <- merge( mydf, probs, by="position")
``````
``````ggplot( mydf, aes(x=position, y=prob)) +
geom_point() +
geom_smooth(method = "glm",
method.args = list(family = "binomial"),
se = FALSE)
``````
``````librayr(plyr)
mydf <- ddply( mydf, "position", mutate, prob = mean(response)  )
``````
``````ggplot( probs, aes(x=position, y=prob)) +
geom_point() +
geom_smooth(data = mydf, aes(x = position, y = response),
method = "glm", method.args = list(family = "binomial"),
se = FALSE)
``````

## Getting the regression line to plot from a Pandas regression

By : Marina
Date : March 29 2020, 07:55 AM
Any of those help Check what values you have in your arrays and variables.
My guess is that your x is just nans, because you use Python's min and max. At least that happens with the version of Pandas that I have currently open.
code :
``````>>> x = pd.Series([np.nan,2], index=['const','slope'])
>>> x
const   NaN
slope     2
dtype: float64

>>> min(x)
nan
>>> max(x)
nan

>>> x.min()
2.0
>>> x.max()
2.0
``````

## scatter plot with single regression line instead of multiple regression lines

By : Zane
Date : March 29 2020, 07:55 AM
this one helps. In geom_smooth add color="black" (or whatever color you want) outside of aes. This will override the color aesthetic specified in the call to ggplot and result in a single regression line.
code :
``````geom_smooth(color="black", method="lm", se=FALSE, linetype="solid", fullrange=TRUE) +
``````
``````annotate(x=20, y=5, label=paste("R = ", round(cor(df\$x1, df\$value),2)), geom="text", size=4)
``````

## Unable to plot regression line correctly with scatter plot in Python

By : Gan Dul
Date : March 29 2020, 07:55 AM
seems to work fine In you drawLine function I changed ax.scatter to plt.scatter. I also changed roomBoard and accStudent to numpy arrays instead of pandas.Series. Finally I changed how you were updating the "private" column to
code :
``````X.loc[:, "Private"] = X.Private.map({'Yes':1, 'No':0})
``````
``````import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import linear_model
from sklearn.model_selection import train_test_split

matplotlib.style.use('ggplot') # Look Pretty

# Wrangling data
X.loc[:, "Private"] = X.Private.map({'Yes':1, 'No':0})

# Splitting data
roomBoard = X.loc[:, 'Room.Board'].values.reshape((len(X),1))
accStudent = X.loc[:, 'Accept'].values.reshape((len(X),1))
X_train, X_test, y_train, y_test = train_test_split(roomBoard, accStudent, test_size=0.3, random_state=7)

# Training model
model = linear_model.LinearRegression()
model.fit(X_train, y_train)
score = model.score(X_test, y_test)

# Visualise results
def drawLine(model, X_test, y_test, title, R2):
fig = plt.figure()
plt.scatter(X_test, y_test, c='g', marker='o')
y_pred =  model.predict(X_test)
plt.plot(X_test, y_pred, color='orange', linewidth=1, alpha=0.7)

title += " R2: " + str(R2)
ax.set_title(title)
print(title)
print("Intercept(s): ", model.intercept_)

plt.xticks(())
plt.yticks(())

plt.show()

drawLine(model, X_test, y_test, "Accept(Room&Board)", score)
``````

## Plot regression line on a scatter plot from regression coefficients

By : John Buckingham
Date : March 29 2020, 07:55 AM
wish helps you Looks like the library is built so that you can pass its return res to a d3.line generator:
code :
``````...
let line = d3.line()
.x((d) => x(d[0]))
.y((d) => y(d[1]));

svg.append("path")
.datum(res)
.attr("d", line)
...
``````
``````<!DOCTYPE html>
<html>

<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/HarryStevens/d3-regression@master/dist/d3-regression.min.js"></script>

<body>
<script>
let dataLinear = [{
x: 8,
y: 3
}, {
x: 2,
y: 10
}, {
x: 11,
y: 3
}, {
x: 6,
y: 6
}, {
x: 5,
y: 8
}, {
x: 4,
y: 12
}, {
x: 12,
y: 1
}, {
x: 9,
y: 4
}, {
x: 6,
y: 9
}, {
x: 1,
y: 14
}]

let linearRegression = d3.regressionLinear()
.x(d => d.x)
.y(d => d.y)
.domain([-1.7, 16]);
let res = linearRegression(dataLinear)

let margin = {
top: 30,
right: 20,
bottom: 30,
left: 50
},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;

let x = d3.scaleLinear().range([0, width]);
let y = d3.scaleLinear().range([height, 0]);

let svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");

x.domain(d3.extent(dataLinear, (d) => d.x));
y.domain(d3.extent(dataLinear, (d) => d.y));

svg.selectAll("circle")
.data(dataLinear)
.enter()
.append("circle")
.attr("r", 5)
.style("fill", "steelblue")
.attr("cx", (d) => x(d.x))
.attr("cy", (d) => y(d.y));

let line = d3.line()
.x((d) => x(d[0]))
.y((d) => y(d[1]));

svg.append("path")
.datum(res)
.attr("d", line)
.style("stroke", "steelblue")
.style("stroke-width", "2px");

svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));

svg.append("g")
.call(d3.axisLeft(y));
</script>
</body>

</html>``````
``````<!DOCTYPE html>
<html>

<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/HarryStevens/d3-regression@master/dist/d3-regression.min.js"></script>

<body>
<script>
let dataLinear = [{
x: 8,
y: 3
}, {
x: 2,
y: 10
}, {
x: 11,
y: 3
}, {
x: 6,
y: 2
}, {
x: 5,
y: 2
}, {
x: 4,
y: 12
}, {
x: 12,
y: 1
}, {
x: 9,
y: 4
}, {
x: 6,
y: 9
}, {
x: 1,
y: 14
}]

.x(d => d.x)
.y(d => d.y)
.domain([-1.7, 16]);

let margin = {
top: 30,
right: 20,
bottom: 30,
left: 50
},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;

let x = d3.scaleLinear().range([0, width]);
let y = d3.scaleLinear().range([height, 0]);

let svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");

x.domain(d3.extent(dataLinear, (d) => d.x));
y.domain(d3.extent(dataLinear, (d) => d.y));

svg.selectAll("circle")
.data(dataLinear)
.enter()
.append("circle")
.attr("r", 5)
.style("fill", "steelblue")
.attr("cx", (d) => x(d.x))
.attr("cy", (d) => y(d.y));

let line = d3.line()
.x((d) => x(d[0]))
.y((d) => y(d[1]));

svg.append("path")
.datum(res)
.attr("d", line)
.style("fill", "none")
.style("stroke", "steelblue")
.style("stroke-width", "2px");

svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));

svg.append("g")
.call(d3.axisLeft(y));
</script>
</body>

</html>``````