linear-regression

Plotting Pandas OLS linear regression results

南楼画角 提交于 2019-12-04 08:48:27
How would I plot my linear regression results for this linear regression I did from pandas? import pandas as pd from pandas.stats.api import ols df = pd.read_csv('Samples.csv', index_col=0) control = ols(y=df['Control'], x=df['Day']) one = ols(y=df['Sample1'], x=df['Day']) two = ols(y=df['Sample2'], x=df['Day']) I tried plot() but it did not work. I want to plot all three samples on one plot are there any pandas code or matplotlib code to hadle data in the format of these summaries? Anyways the results look like this: Control ------------------------Summary of Regression Analysis--------------

Fast group-by simple linear regression

蹲街弑〆低调 提交于 2019-12-04 07:54:21
This Q & A arises from How to make group_by and lm fast? where OP was trying to do a simple linear regression per group for a large data frame. In theory, a series of group-by regression y ~ x | g is equivalent to a single pooled regression y ~ x * g . The latter is very appealing because statistical test between different groups is straightforward. But in practice doing this larger regression is not computationally easy. My answer on the linked Q & A reviews packages speedlm and glm4 , but pointed out that they can't well address this problem. Large regression problem is difficult,

Piecewise regression with a straight line and a horizontal line joining at a break point

99封情书 提交于 2019-12-04 07:14:52
I want to do a piecewise linear regression with one break point, where the 2nd half of the regression line has slope = 0 . There are examples of how to do a piecewise linear regression, such as here . The problem I'm having is I'm not clear how to fix the slope of half of the model to be 0. I tried lhs <- function(x) ifelse(x < k, k-x, 0) rhs <- function(x) ifelse(x < k, 0, x-k) fit <- lm(y ~ lhs(x) + rhs(x)) where k is the break point, but the segment on the right is not a flat / horizontal one. I want to constrain the slope of the second segment at 0. I tried: fit <- lm(y ~ x * (x < k) + x *

Linear regression with tensorflow

ε祈祈猫儿з 提交于 2019-12-04 05:17:34
I trying to understand linear regression... here is script that I tried to understand: ''' A linear regression learning algorithm example using TensorFlow library. Author: Aymeric Damien Project: https://github.com/aymericdamien/TensorFlow-Examples/ ''' from __future__ import print_function import tensorflow as tf from numpy import * import numpy import matplotlib.pyplot as plt rng = numpy.random # Parameters learning_rate = 0.0001 training_epochs = 1000 display_step = 50 # Training Data train_X = numpy.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167, 7.042,10.791,5.313,7.997,5.654

Solving normal equation gives different coefficients from using `lm`?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 05:01:44
问题 I wanted to compute a simple regression using the lm and plain matrix algebra. However, my regression coefficients obtained from matrix algebra are only half of those obtained from using the lm and I have no clue why. Here's the code boot_example <- data.frame( x1 = c(1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), x2 = c(0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L), x3 = c(1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L), x4 = c(0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L), x5 = c(1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L), x6 = c(0L, 1L,

Parallelising gradient calculation in Julia

冷暖自知 提交于 2019-12-04 04:19:37
I was persuaded some time ago to drop my comfortable matlab programming and start programming in Julia. I have been working for a long with neural networks and I thought that, now with Julia, I could get things done faster by parallelising the calculation of the gradient. The gradient need not be calculated on the entire dataset in one go; instead one can split the calculation. For instance, by splitting the dataset in parts, we can calculate a partial gradient on each part. The total gradient is then calculated by adding up the partial gradients. Though, the principle is simple, when I

R: plm — year fixed effects — year and quarter data

守給你的承諾、 提交于 2019-12-04 04:08:28
I am having a problem setting up a panel data model. Here is some sample data: library(plm) id <- c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2) year <- c(1999,1999,1999,1999,2000,2000,2000,2000,1999,1999,1999,1999,2000,2000,2000,2000) qtr <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4) y <- rnorm(16, mean=0, sd=1) x <- rnorm(16, mean=0, sd=1) data <- data.frame(id=id,year=year,qtr=qtr,y_q=paste(year,qtr,sep="_"),y=y,x=x) I run the following regression using 'id' as the individual index and 'year' as the time index: reg1 <- plm(y ~ x, data=data,index=c("id", "year"), model="within",effect="time") Unfortunately, I

Create lm object from data/coefficients

狂风中的少年 提交于 2019-12-04 03:55:52
Does anyone know of a function that can create an lm object given a dataset and coefficients? I'm interested in this because I started playing with Bayesian model averaging (BMA) and I'd like to be able to create an lm object out of the results of bicreg. I'd like to have access to all of the nice generic lm functions like diagnostic plotting, predict, cv.lm etc. If you are pretty sure such a function doesn't exist that's also very helpful to know! library(BMA) mtcars_y <- mtcars[, 1] #mpg mtcars_x <- as.matrix(mtcars[,-1]) res <- bicreg(mtcars_x, mtcars_y) summary(res) res$postmean # bma

Seaborn: annotate the linear regression equation

余生颓废 提交于 2019-12-04 02:27:52
I tried fitting an OLS for Boston data set. My graph looks like below. How to annotate the linear regression equation just above the line or somewhere in the graph? How do I print the equation in Python? I am fairly new to this area. Exploring python as of now. If somebody can help me, it would speed up my learning curve. Many thanks! I tried this as well. My problem is - how to annotate the above in the graph in equation format? You can use coefficients of linear fit to make a legend like in this example: import seaborn as sns import matplotlib.pyplot as plt from scipy import stats tips = sns

How to model polynomial regression in R?

梦想的初衷 提交于 2019-12-04 01:42:58
问题 I've a dataset with 70 variables, and I want to try polynomial regression on it. If the number of columns were three/four I could just hand code something like this -- model <- lm(y ~ poly(var1,3) + poly(var2,3) + poly(var4,4) How would we go about this, if we have 70 variables? Should we type in manually names of all the variables or is there a easier method? 回答1: You could paste the formula, if all variables are named systematically: form <- as.formula(paste("y~", paste0("poly(var", 1:10, "