r-factor

Logistic Regression on factor: Error in eval(family$initialize) : y values must be 0 <= y <= 1

五迷三道 提交于 2019-12-26 07:42:14
问题 Not able to fix the below error for the below logistic regression training=(IBM$Serial<625) data=IBM[!training,] dim(data) stock.direction <- data$Direction training_model=glm(stock.direction~data$lag2,data=data,family=binomial) ###Error### ---- Error in eval(family$initialize) : y values must be 0 <= y <= 1 Few rows from the data i am using X Date Open High Low Close Adj.Close Volume Return lag1 lag2 lag3 Direction Serial 1 28-11-2012 190.979996 192.039993 189.270004 191.979996 165.107727

R and factor coding in formula

廉价感情. 提交于 2019-12-25 08:19:52
问题 How do I use the formula interface if I want custom valued dummies, e.g. if I want values 1 and two, rather than 0 and 1. The estimation might look like the following where supp is a factor variable. fit <- lm(len ~ dose + supp, data = ToothGrowth) In this example, there is not much use of the different values, but in many cases of a "re-written" model it can be useful. EDIT: Actually, I have e.g. 3 levels, and want the two columns to be coded differently, so one is a 1/0 variable, and the

Factor, levels, and original values

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 07:49:33
问题 I would like to write variable f into certain elements ( index ) of an existing matrix m . Let's assume f is a factor: f <- factor(c(3,3,0,3,0)) m <- matrix(NA, 10, 1) index <- c(1,4,5,8,9) Using m[index] <- f does not give the desired result as it puts the labels ('1' and '2') into m but not the original values ('0' and '3'). Therefore, I used m[index] <- as.numeric(levels(f))[f] instead, which works well. But in my situation, f is not always a factor but can also be numeric like f <- c(3.43

How to access actual internal factor lookup hashtable in R

☆樱花仙子☆ 提交于 2019-12-24 13:05:57
问题 Dear Stackoverflow community, I have looked everywhere but can't find the answer to this question. I am trying to access the factor lookup table that R uses when you change a string vector into a factor vector. I am not trying to convert a string to a factor but rather to get the lookup table underlying the factor variable and store it as a hash table for use elsewhere. I encountered the problem because I want to use this factor lookup table on a list of different length vectors, to convert

Change level of multiple factor variables

狂风中的少年 提交于 2019-12-24 10:57:31
问题 everyone - I want to preface this by saying that I already looked at this link to try to solve my problem: Applying the same factor levels to multiple variables in an R data frame The difference is that in that problem, the OP wanted to change the levels of factors that all had the same levels. In my instance, I'm looking to change just the first level, which is set to ' ', to something like 'Unknown' and leave the rest of the levels alone. I know I could do this in a "non-R" way with

Getting Factor Means into the dataset after calculation

霸气de小男生 提交于 2019-12-24 09:10:09
问题 I am trying to create a normalization value for a variable I am working with based on individual conference means and SDs. I found the conference means using the function: confavg=aggregate(base$AVG, by=list(base$confName), FUN=mean) And so after getting the means for the 31 conferences, I want to go back and for each individual player put these means in so I can easily calculate a normalization factor based on the conference mean. I have tried to create large ifelse or if statements where

R: Legend color according to factor levels

女生的网名这么多〃 提交于 2019-12-24 08:13:22
问题 Inspired by this question where apparently the top answer is using an unsafe/erroneous way to add colors to a legend for a scatter plot. Top answer suggests doing this: data<-iris plot(data$Sepal.Length, data$Sepal.Width, col=data$Species) legend(7,4.3,unique(data$Species),col=1:length(data$Species),pch=1) Comments suggest using levels() instead of unique() for controlling the text and colors in the call to legend() , but are unclear on why it would help. I would need a better explanation to

R data.frame: rowSums of selected columns by grouping vector

不羁岁月 提交于 2019-12-24 03:33:12
问题 I have a data frame with a sequence of numeric columns, surrounded on both sides by (irrelevant) columns of characters. I want to obtain a new data frame that keeps the position of the irrelevant columns, and adds the numeric columns to eachother by a certain grouping vector (or applies some other row-wise function to the data frame, by group). Example: sample = data.frame(cha1 = c("A","B"),num1=1:2,num2=3:4,num3=11:12,num4=13:14,cha2=c("C","D")) > sample cha1 num1 num2 num3 num4 cha2 1 A 1 3

dplyr and tidyr: convert long to wide format and arrange columns

人走茶凉 提交于 2019-12-24 00:57:38
问题 I'm creating a shiny app in which the user will upload a .csv file that contains several variables. Using dplyr , I will select the first four variables, shown below, and convert them from long format. DATA df <- read.table(text = c(" Customer Rate Factor Power W1 6 TK1 5 W2 3 TK1 0 W3 1 TK1 0 W4 2 TK1 0 W5 4 TK1 0 W6 8 TK1 0 W7 5 TK1 0 W8 7 TK1 3 W1 6 TK2 0 W2 3 TK2 1 W3 1 TK2 0 W4 2 TK2 5 W5 4 TK2 0 W6 8 TK2 0 W7 5 TK2 0 W8 7 TK2 3 W1 6 TK3 0 W2 3 TK3 5 W3 1 TK3 1 W4 2 TK3 0 W5 4 TK3 0 W6 8

Why doesn't rle accept a factor as input?

ε祈祈猫儿з 提交于 2019-12-23 11:53:37
问题 I'm having trouble passing this rle function on a data.frame . Function works great on another set: fgroup <- aggregate(fevents2[,3:14], list(weeks = fevents2[, 1]), function(x) rle(x)$values) Which yields the error: Error in rle(x) : 'x' must be an atomic vector Sample data: > dput(fevents2[1:20,]) structure(list(weeks = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3", "4", "5", "6", "7"), class = "factor"), A1M.Date =