[R] matrix with standard errors of lm model
Hi Can someone give me a hint on how to create a matrix with standard errors from lm model? I have already managed to get the matrix with coefficients: coef-as.data.frame(sapply(seq_len(ncol(es.w)),function( i) {x1- summary(lm(es.w[,i]~es.median[,i]));x1$coef[,1]})) but I can't get the one like this for standard errors. I do regression for each column. Thanks a lot :) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] t.test error
Hi I receive a very strange error message after trying to do t-test. When I write the code t.test(x) I get an error message: error in t.test(x) : function sqr not found I don't understand this problem. Can someone help me how to do it right? Thanks a lot :) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] replace Na values with the mean of the column which contains them
Hi everyone I have a problem with replacing the NA values with the mean of the column which contains them. If I replace Na with the means of the rest values in the column, the mean of the whole column will be still the same as if I would have omitted NA values. I have the following data de [,1][,2] [,3] [1,] NA -0.26928087 -0.1192078 [2,] NA 1.20925752 0.9325334 [3,] NA 0.38012008 -1.8927164 [4,] NA -0.41778861 1.4330507 [5,] NA -0.49677462 0.2892706 [6,] NA -0.13248754 1.3976522 [7,] NA -0.54179054 0.2295291 [8,] NA 0.35788624 -0.5009389 [9,] 0.27500571 -0.41467591 -0.3426560 [10,] -3.07568579 -0.59234248 -0.8439027 [11,] -0.42240954 0.73642396 -0.4971999 [12,] -0.26901731 -0.06768044 -1.6127122 [13,] 0.01766284 -0.40321968 -0.6508823 [14,] -0.80999580 -1.52283305 1.4729576 [15,] 0.20805934 0.25974308 -1.6093478 [16,] 0.03036708 -0.04013730 0.1686006 and I wrote the code de[which(is.na(de))]-sapply(seq_len(ncol(de)),function(i) {mean(de[,i],na.rm=TRUE)}) I get as the result [,1][,2] [,3] [1,] -0.50575168 -0.26928087 -0.1192078 [2,] -0.1376 1.20925752 0.9325334 [3,] -0.13412312 0.38012008 -1.8927164 [4,] -0.50575168 -0.41778861 1.4330507 [5,] -0.1376 -0.49677462 0.2892706 [6,] -0.13412312 -0.13248754 1.3976522 [7,] -0.50575168 -0.54179054 0.2295291 [8,] -0.1376 0.35788624 -0.5009389 [9,] 0.27500571 -0.41467591 -0.3426560 [10,] -3.07568579 -0.59234248 -0.8439027 [11,] -0.42240954 0.73642396 -0.4971999 [12,] -0.26901731 -0.06768044 -1.6127122 [13,] 0.01766284 -0.40321968 -0.6508823 [14,] -0.80999580 -1.52283305 1.4729576 [15,] 0.20805934 0.25974308 -1.6093478 [16,] 0.03036708 -0.04013730 0.1686006 It has replaced the NA values in first column with mean of first column -0.505... and second cell with mean of second column etc. I want to have the result like this: [,1][,2] [,3] [1,] -0.50575168 -0.26928087 -0.1192078 [2,] -0.50575168 1.20925752 0.9325334 [3,] -0.50575168 0.38012008 -1.8927164 [4,] -0.50575168 -0.41778861 1.4330507 [5,] -0.50575168 -0.49677462 0.2892706 [6,] -0.50575168 -0.13248754 1.3976522 [7,] -0.50575168 -0.54179054 0.2295291 [8,] -0.50575168 0.35788624 -0.5009389 [9,] 0.27500571 -0.41467591 -0.3426560 [10,] -3.07568579 -0.59234248 -0.8439027 [11,] -0.42240954 0.73642396 -0.4971999 [12,] -0.26901731 -0.06768044 -1.6127122 [13,] 0.01766284 -0.40321968 -0.6508823 [14,] -0.80999580 -1.52283305 1.4729576 [15,] 0.20805934 0.25974308 -1.6093478 [16,] 0.03036708 -0.04013730 0.1686006 Thanks in advance __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] linear fit function with NA values
Hi Quick question. I am running a multiple regression function for each column of two data sets. That means as a result I get several coefficients. I have a problem because data that I use for regression contains NA. How can I ignore NA in lm function. I use the following code for regression: OLS-lapply(seq_len(ncol(es.w)),function(i) {lm(es.w[,i]~es.median[,i])}) as response I get Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : all values NA thanks for help :) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] linear fit function with NA values
Hi Thanks for your hints. I would like to describe my problem better and give an examle of the data that I use. I conduct the event study and I need to create abnormal returns for the daily stock prices. I have for each stock returns from time period of 8 years. For some days I don't have the data for many reasons. in excel file they are just empty cells but I convert my data into 'zoo' and then it is transformed into NA. I get something like this return ATIAMU -1 0.734 9.003 00.999 2.001 13.097 -1.003 2NANA 3NA 3.541 median ATIAMU -1 3.224 -2.003 02.999 -1.301 11.3-1.003 24.000 2.442 3 -10 4.511 I want to regress first column return with first column median and second column return with second column median. when I do OLS-lapply(seq_len(ncol(return)),function(i) {lm(return[,i]~median[,i])}) I get an error message. I would like my function to omit the NAs and for example for ATI returns to take into account only the values for -1,0,1 and regress it against the same values from ATI in median which means it would also take only (3.224, 2.999, 1.3) Is it possible to do it? Thanks a lot W dniu 2013-07-27 17:33:30 użytkownik arun smartpink...@yahoo.com napisał: HI, set.seed(28) dat1- as.data.frame(matrix(sample(c(NA,1:20),100,replace=TRUE),ncol=10)) set.seed(49) dat2- as.data.frame(matrix(sample(c(NA,40:80),100,replace=TRUE),ncol=10)) lapply(seq_len(ncol(dat1)),function(i) {lm(dat2[,i]~dat1[,i])}) #works bcz the default setting removes NA Regarding the options: ?lm() na.action: a function which indicates what should happen when the data contain ‘NA’s. The default is set by the ‘na.action’ setting of ‘options’, and is ‘na.fail’ if that is unset. The ‘factory-fresh’ default is ‘na.omit’. Another possible value is ‘NULL’, no action. Value ‘na.exclude’ can be useful. lapply(seq_len(ncol(dat1)),function(i) {lm(dat2[,i]~dat1[,i],na.action=na.exclude)}) #or lapply(seq_len(ncol(dat1)),function(i) {lm(dat2[,i]~dat1[,i],na.action=na.omit)}) lapply(seq_len(ncol(dat1)),function(i) {lm(dat2[,i]~dat1[,i],na.action=na.fail)}) #Error in na.fail.default(list(`dat2[, i]` = c(54L, 59L, 50L, 64L, 40L, : # missing values in object In your case, the error is different. It could be something similar to the below case: dat1[,1]- NA lapply(seq_len(ncol(dat1)),function(i) {lm(dat2[,i]~dat1[,i],na.action=na.omit)}) #Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : # 0 (non-NA) cases # here it is different lapply(seq_len(ncol(dat1)),function(i) {try(lm(dat2[,i]~dat1[,i]))}) #works in the above case. It may not work in your case. You need to provide a reproducible example to understand the situation better. A.K. - Original Message - From: iza.ch1 iza@op.pl To: r-help@r-project.org Cc: Sent: Saturday, July 27, 2013 8:47 AM Subject: [R] linear fit function with NA values Hi Quick question. I am running a multiple regression function for each column of two data sets. That means as a result I get several coefficients. I have a problem because data that I use for regression contains NA. How can I ignore NA in lm function. I use the following code for regression: OLS-lapply(seq_len(ncol(es.w)),function(i) {lm(es.w[,i]~es.median[,i])}) as response I get Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : all values NA thanks for help :) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] do not perform function if the outcome is NA
Hi you all I have a question regarding the function. In my function I divide the values by the standard errors and sometimes the standard error is equal to zero and I get the result NA. Can I write the function in the way that if the outcome of the function is zero then the function is not conducted and it stays the value (not divided by standard errors)? the code for my function is the following: standardised.abnormal.returns-lapply(seq_len(ncol(abnormal.returns)),function(i) {abnormal.returns[,i]/standard.deviation[i,1]}) Thank you all for the help __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] do not perform function if the outcome is NA
Hi Rui Thanks a lot. It works perfect. Izzie Hello, Try the following. standardised.abnormal.returns - lapply(seq_len(ncol(abnormal.returns)),function(i) { if(standard.deviation[i,1] == 0) abnormal.returns[,i] else abnormal.returns[,i]/standard.deviation[i,1] }) Hope this helps, Rui Barradas Em 23-07-2013 22:58, iza.ch1 escreveu: Hi you all I have a question regarding the function. In my function I divide the values by the standard errors and sometimes the standard error is equal to zero and I get the result NA. Can I write the function in the way that if the outcome of the function is zero then the function is not conducted and it stays the value (not divided by standard errors)? the code for my function is the following: standardised.abnormal.returns-lapply(seq_len(ncol(abnormal.returns)),function(i) {abnormal.returns[,i]/standard.deviation[i,1]}) Thank you all for the help __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] create data frame with coefficients from many regressions
Hi ! I want to ask if somebody knows the way to create data frame with coefficients from many regressions I regress the first column from ret against the first columns from median, then the second with the second and so on. This is the code used for regression i-1:6 lapply(seq_len(ncol(ret)),function(i) {lm(ret[,i]~median[,i])} I get 6 results for each regression [[1]] Call: lm(formula = ret[, i] ~ median[, i]) Coefficients: (Intercept) median[, i] 01 [[2]] Call: lm(formula = ret[, i] ~ median[, i]) Coefficients: (Intercept) median[, i] -1.411e-171.000e+00 now I would like to create a data frame with intercepts which looks like it [[1]] [[2]] Intercept median I tried to use ddply command but it does not work. I will be very grateful for the hint :) Thank you in advance __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Linear regression repeat for each column
Hi everyone I need to calculate abnormal returns for different events applying event study methodology. I must create a market model in order to perform the analysis. I apply regression analysis to get OLS estimators. I have a problem to create a linear regression which I could repeat for each column in two different data frames (one with explainatory and one with explaning variables). It means that I want to regress column 1 from first data frame with column 1 from second data frame,, clumn two with column two etc. I tried to use the following code:#x is matrix containing stock returns y is matrix containing market index i-1:length(x) t[i] - lapply(t[i], lm(x[,1]~y[,i])) but it is not working. Could anybody help me? Thanks a lot Iza __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.