linear-regression

Finding the slope for multiple points in selected columns

情到浓时终转凉″ 提交于 2019-12-13 14:40:16
问题 Given the following data frame: structure(list(`-5` = c(0, 1, 0, 0, 9, 22), `-4` = c(1, 3, 0, 0, 1, 17), `-3` = c(1, 3, 0, 0, 0, 12), `-2` = c(1, 3, 0, 0, 2, 10), `-1` = c(0, 0, 0, 4, 3, 9), `0` = c(0, 1, 0, 2, 2, 21 ), `1` = c(0, 1, 1, 7, 1, 21), `2` = c(1, 0, 1, 2, 1, 10), `3` = c(0, 9, 0, 6, 1, 12), `4` = c(0, 2, 0, 5, 0, 18), `5` = c(0, 0, 0, 3, 0, 23)), .Names = c("-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5"), row.names = c(NA, 6L), class = "data.frame") # -5 -4 -3 -2 -1 0

Tensorflow on simple linear regression

折月煮酒 提交于 2019-12-13 13:26:57
问题 I am a beginner in machine learning and tensorflow. In the first step trying the tensorflow, I tried a simple multivariate linear regression. However, it seems the model stuck at a local minimum. Here is my code. import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=1) return tf.Variable(initial) # dataset xx = np.random.randint(0,1000,[1000,3])/1000. yy = xx[:,0] * 2 + xx[:,1] * 1.4 + xx[:,2] * 3 #

R - Linear Regression - Control for a variable

前提是你 提交于 2019-12-13 12:23:09
问题 This question was migrated from Cross Validated because it can be answered on Stack Overflow. Migrated 5 years ago . I have a computer science background & I am trying to teach myself data science by solving the problems available on the internet I have a smallish data set which has 3 variables - race, gender and annual income. There are about 10,000 sample observations. I am trying to predict income from race & gender. I have divided the data into 2 parts - one for each gender & now I am

Gradient descent and normal equation method for solving linear regression gives different solutions

人走茶凉 提交于 2019-12-13 11:49:49
问题 I'm working on machine learning problem and want to use linear regression as learning algorithm. I have implemented 2 different methods to find parameters theta of linear regression model: Gradient (steepest) descent and Normal equation. On the same data they should both give approximately equal theta vector. However they do not. Both theta vectors are very similar on all elements but the first one. That is the one used to multiply vector of all 1 added to the data. Here is how the theta s

Generate an array of regression models without for loop

让人想犯罪 __ 提交于 2019-12-13 11:22:20
问题 I have a data set with columns Y, X1, X2 and V. While Y, X1 and X2 are continuous, V is a categorical variable. Assuming V has 10 categories, I want to create 10 linear regression models and store the results (coefficients, p-values, R-Sq, etc) in another table. Is there a way to do it with data.table without using for loops? Thanks. 回答1: The base R function by is what you want. # make up some sample data dataSet <- data.frame(Y = iris$Sepal.Length, X1 = iris$Sepal.Width, X2 = iris$Petal

R abline() equivalent in Python

感情迁移 提交于 2019-12-13 10:29:43
问题 I am trying to plot a Linear Regression onto a scatterplot in Python. In R I would simply do the following: Run OLS Linear Regresion fit_1 <- lm(medv ~ lstat) plot(medv ~ lstat) abline(fit_1, col = "red") I have been looking at different solutions in Python, but I can't seem to be able to actually get it to work. My script is: Plot Data Boston.plot(kind='scatter', x='medv', y='lstat', color = "black") plt.show() Run Linear Regression fit_1 = sm.ols(formula='medv ~ lstat', data= Boston).fit()

How to add linear model results (adj-r squared, slope and p-value) onto regression plot in r

感情迁移 提交于 2019-12-13 10:00:28
问题 Hi I have created a linear model and a regression plot - However, I would like to have the model results on the plot itself - something like the image below: How do I show the key results on the plot? Below is my code for the plot: library(ggplot2) ggplot(HP_crime15, aes (x = as.numeric(HP_crime15$Theft15), y = as.numeric(HP_crime15$X2015))) + geom_point(shape=1) + geom_smooth(method=lm) + xlab ("Recorded number of Thefts") + ylab("House prices (£)") + ggtitle("Title") 回答1: Ideally good

How to extract coefficients outputs from a linear regression with loop

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 08:48:32
问题 I would like to know how I can loop a regression n times, and in each time with a different set of variables, extract a data.frame where each column is a regression and each row represent a variable. In my case I have a data.frame of: dt_deals <- data.frame(Premium=c(1,3,4,5),Liquidity=c(0.2,0.3,1.5,0.8),Leverage=c(1,3,0.5,0.7)) But I have another explanatory dummy variable called hubris , that is the product of a binomial distribution, with 0.25 of mean. Like that: n <- 10 hubris_dataset <-

Mutable Vector field is not updating in F#

删除回忆录丶 提交于 2019-12-13 07:37:19
问题 let gradientDescent (X : Matrix<double>) (y :Vector<double>) (theta : Vector<double>) alpha (num_iters : int) = let J_history = Vector<double>.Build.Dense(num_iters) let m = y.Count |> double theta.At(0, 0.0) let x = (X.Column(0).PointwiseMultiply(X*theta-y)) |> Vector.sum for i in 0 .. (num_iters-1) do let next_theta0 = theta.[0] - (alpha / m) * ((X.Column(0).PointwiseMultiply(X*theta-y)) |> Vector.sum) let next_theta1 = theta.[1] - (alpha / m) * ((X.Column(1).PointwiseMultiply(X*theta-y)) |

statsmodels — weights in robust linear regression

半世苍凉 提交于 2019-12-13 07:26:39
问题 I was looking at the robust linear regression in statsmodels and I couldn't find a way to specify the "weights" of this regression. For example in least square regression assigning weights to each observation. Similar to what WLS does in statsmodels. Or is there a way to get around it? http://www.statsmodels.org/dev/rlm.html 回答1: RLM currently does not allow user specified weights. Weights are internally used to implement the reweighted least squares fitting method. If the weights have the