I am new to Rstudio and Stack and am trying to calculate the AIC for my model with 8 variables (One dummy) but only 10 observations (Not sure if this is the issue). The code I have run so far is:
library("MASS")
energy_data <- read.table('energy.txt', header = T)
full <- lm(y~., data = energy_data)
Following this, I get the warning
Warning messages:
1: In model.response(mf, "numeric") :
using type = "numeric" with a factor response will be ignored
2: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
I have looked at other forum threads but I am not sure if they apply in my case.
I have also tried to run this as a CSV file but in this case I get the error:
Error in model.frame.default(formula = y ~ ., data = Energy, drop.unused.levels = TRUE) : variable lengths differ (found for 'GSP') where GSP is x1
Again this has been asked before but I am unsure how to address in my case.txt data CSV data
Thanks in advance!
The problem is the ,
in the y
column, which causes it to be a factor. You can fix it by removing the ,
using gsub
, but in order to do that you have to change it to character first. Once you remove the ,
, you'll need to change it to numeric.
library(MASS)
energy_data <- read.table(text="x1 x2 x3 x4 x5 x6 x7 x8 y
361 79.7 2.983013699 1.683972603 4.35 2573.2 2048 0 77,465.59
369 86.9 2.814684932 1.701424658 4.7 2599 2094.5 0 76,705.04
375 100 2.780956284 2.006202186 3.7 2634.2 2141 0.5 72,704.99
384 119.3 3.594931507 1.448 2.75 2669.4 2266 1 70,061.93
392 123.5 3.354246575 1.522657534 2.5 2704.6 2391 0.5 69,587.52
402 114.5 3.17438562 1.653780822 2.1 2739.8 2418 0 70,138.85
410 109.3 3.691912568 1.346284153 1.75 2775 2445 0 70,405.99
418 120.8 3.725232877 1.818520548 1.5 2859.7 2586 0 70,879.50
421 136.6 3.681369863 1.79890411 1.5 2944.4 2726 0 70,436.26
431 135.2 3.960821918 1.959369863 1.2 3029.1 2867 0 70,161.83", header=T, sep="\t")
energy_data$y <- as.numeric(gsub(",","",as.character(energy_data$y)))
full <- lm(y~., data = energy_data)
You can also solve it by editing the file you read in to remove ,
.
energy_data <- read.table(text="x1 x2 x3 x4 x5 x6 x7 x8 y
361 79.7 2.983013699 1.683972603 4.35 2573.2 2048 0 77465.59
369 86.9 2.814684932 1.701424658 4.7 2599 2094.5 0 76705.04
375 100 2.780956284 2.006202186 3.7 2634.2 2141 0.5 72704.99
384 119.3 3.594931507 1.448 2.75 2669.4 2266 1 70061.93
392 123.5 3.354246575 1.522657534 2.5 2704.6 2391 0.5 69587.52
402 114.5 3.17438562 1.653780822 2.1 2739.8 2418 0 70138.85
410 109.3 3.691912568 1.346284153 1.75 2775 2445 0 70405.99
418 120.8 3.725232877 1.818520548 1.5 2859.7 2586 0 70879.50
421 136.6 3.681369863 1.79890411 1.5 2944.4 2726 0 70436.26
431 135.2 3.960821918 1.959369863 1.2 3029.1 2867 0 70161.83", header=T, sep="\t")
full <- lm(y~., data = energy_data)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments